Saturday, December 27, 2014

Headless Browser Test in Selenium Webdriver



 Why Headless Browser?

There are times when you don't want a browser popping each time you execute the test.

1.     Lets assume our test running in the continuous integration environment Like
  • jenkins
  • Banboo
Here all test will run on server.So no need any Physical things need to be done like (opening browser).We get result in Logs file at end of test.In this situation we no need to open browser .

  2.We made it to run locally also.
  3.Headless browser is very fast to excute.

In this Program:
·       Navigating to http://automationplace.blogspot.in
·       Verifying the title

Without Headless Browser

Sourcecode :

import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;

public class test_headlessbrowser {
      
       @Test
       public void headlessBrowser()
       {
              WebDriver driver=driver = new FirefoxDriver()
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.automationplace.blogspot.in");
       
        String title=driver.findElement(By.xpath("//*[@id='header-inner']/div[1]/h1")).getText();
        //this will verify my page title
        System.out.println(title);
        Assert.assertTrue(title.contains("Automationplace"));
       
       }
}
      

Note : The Execution is slow comparing to Headless Browser


The Execution will take 26.591 sec.


WithHeadless Browser:

To run in headless browser we need to change only one thing in the above code.

i.e)WebDriver driver=driver = new HtmlUnitDriver();

The rest of the things is same.

Source code :

import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;

public class test_headlessbrowser {
      
       @Test
       public void headlessBrowser()
       {
              WebDriver driver=driver = new HtmlUnitDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.automationplace.blogspot.in");
       
        String title=driver.findElement(By.xpath("//*[@id='header-inner']/div[1]/h1")).getText();
        //this will verify my page title
        System.out.println(title);
        Assert.assertTrue(title.contains("Automationplace"));
       
       }
}
  

If you Run the above code



The Execution will take only 7.738 sec


Hope you like this post.



    













Friday, December 26, 2014

PageFactory in Selenium WebDriver



Why Page Factory?

·      Page Factory is an inbuilt page object model concept for Selenium Webdriver

·      Here as well we follow the concept of separation of Page Object repository and Test methods.


·        To support Page Object Pattern  the package org.openqa.selenium’ has a PageFactory Class
·        To import Pagefactory class just import
 org.openqa.selenium.support.PageFactory;

·         PageFactory class we use annotations @FindBy to find WebElement.
·        @FindBy can accept tagName, partialLinkText, name, linkText, id, css, className, xpath as attributes.

See the below screenshot



Let take our the previous POM Example. but this time am trying to use PageFactory Class to find elements   



In this program am going to do the following scenarios.

1.       Am going to get page title
2.       Assert that page if its correct or not
3.       Clicking on about link
4.       Check the author name is correct or not

aboutpage.java

package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class aboutpage {
WebDriver driver;
//By aboutlink = By.xpath("//table//tr[@class='heading3']");
@FindBy(xpath="//*[@id='Profile1']/div/dl/dt/a")
WebElement aboutlink;
@FindBy(xpath="//*[@id='maia-main']/div/h1")
WebElement authorname;
public aboutpage(WebDriver driver)
{
this.driver=driver;
//This initElements method will create all WebElements
PageFactory.initElements(driver, this);
}
public void clickaboutlink()
{
//driver.findElement(aboutlink).click();
aboutlink.click();
}
public String getauthorname()
{
//return driver.findElement(authorname).getText();
return authorname.getText();
}

}


homepage.java

package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class homepage {
            public WebDriver driver;
           
            @FindBy(xpath="//*[@id='header-inner']/div[1]/h1")
            //By title=By.xpath("//*[@id='header-inner']/div[1]/h1");
            WebElement title;
            //intialize driver using constructor
            public homepage(WebDriver driver)
            {
                        this.driver=driver;
                        PageFactory.initElements(driver, this);
            }
           
            public String getTitlefromHomePage()
            {
                       
                        //return driver.findElement(title).getText();
                        return title.getText();
                       
            }
           

}

Testcase

pom_testcase1.java

package tests;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import pages.aboutpage;
import pages.homepage;

public class pom_testcase1 {
           
            //initialize driver
            WebDriver driver;
            //creating object for pages
            homepage hp;
            aboutpage ap;
           
             @BeforeTest
                public void setup(){
                    driver = new FirefoxDriver();
                    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                    driver.get("http://www.automationplace.blogspot.in");
                }
             
             @Test
             public void test_homepage()
             {
                         hp=new homepage(driver);
                         //verify the title
                         String title=hp.getTitlefromHomePage();
                         //this will verify my page title
                         Assert.assertTrue(title.contains("Automationplace"));
                         System.out.println(title);
                         ap=new aboutpage(driver);
                         //click on about me link in homepage
                         ap.clickaboutlink();
                         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                         String authorname=ap.getauthorname();
                         //check author name in about page
                         Assert.assertTrue(authorname.contains("Prashanth KK"));
                         System.out.println(authorname);
                         System.out.println("Clicked about like");
                         
             }
           

}

That’s it…Run the above program.

See the changes without using PageFactory and with using PageFactory class.in the below image






Hope you like this post.

Hit LIKE button on facebook.