Tuesday, 15 November 2016

Meta description of site in webdriver

Hey 

Today we will be learning that how we can find the description of a site using webdriver script.It is very helpful in that test cases where we have to determine the type of website is about.We search for the content of meta description and get that is about which topic.

It is very easy to determine or get the content of website using selenium functions.In this we will again be using Xpath of the meta tag and then getting its attribute.

Xpath for meta description will be same for every site.But there may be cases where description in meta tag is not there or it is empty so in that case we have to handle it by Exception handling or using try and catch. 

Now lets see the script


SCRIPT FOR GETTING DESCRIPTION OF A WEBSITE


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.FirefoxDriver;


public class metadesc{
public static void main(String args[])
{
WebDriver driver;
driver=new FirefoxDriver();
String base="http://seleniumtestingcom.blogspot.in/";
driver.navigate().to(base);
        String a=driver.findElement(By.xpath("//meta[@name='description']")).getAttribute("content");
    System.out.println(a);
}
}


Lets see the output



meta description of site in webdriver








Friday, 11 November 2016

Verify Title of a webpage using selenium webdriver script

Verify Title of a webpage using selenium webdriver script

Today we will be learning that how we can verify a title of a webpage using script.There can be a testcase where we have to verify the title of a webpage so for this we can use IF condition and functions of String class as well.



SCRIPT FOR VERIFYING TITLE OF A WEBPAGE


import java.io.File;
import org.openqa.selenium.WebDriver;

public class verifytitle
{
public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
String baseurl = "http://seleniumtestingcom.blogspot.in/";
                String expectedtitle = "Automation Testing";
                String actualtitle = "";

              driver.get(baseurl);
             // get the actual value of the title
             actualtitle = driver.getTitle();

            if (actualtitle.contentEquals(expectedtitle))
           {
                  System.out.println("Test Passed!");
            }
           else
           {
                  System.out.println("Test Failed");
           }    
              driver.close();
}


}

In this case we will be getting Test Passed as an output