A web application can be opened in any browser by the user, so we need to ensure that the web application will work as expected in all browsers, that is why we need to do cross browser testing.
Cross browser Testing is a method to test web application with different web browsers.
Selenium supports to run webdriver in browsers by adding path of the driver for the individual browsers.
Now, we will see the setup and execution of drivers in below-mentioned browsers:
1) Mozilla Firefox
2) Google Chrome
3) PhantomJS
4) HTML Unit
The drivers for the mentioned browsers (except HTML Unit) can be downloaded from here- SeleniumHQ
Execution Of Mozilla Firefox Driver
When you are using Selenium V_2.xx , then you do not need to install any additional jar files. It is the default driver that selenium webdriver supports.
But If you are using Selenium 3 then to work with Firefox browser , you need to use a separate driver which will interact with Firefox browser i.e GeckoDriver.
package Firefox; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class geckodriver { WebDriver driver; String gecko_path = null; @BeforeTest public void Setup() { // Set the path of firefox driver in 'gecko_path' variable gecko_path = "path to gecko driver"; //set the property for gecko driver, specify its location via the webdriver.gecko.driver System.setProperty("webdriver.gecko.driver", gecko_path+"geckodriver"); // Declaring and Initializing firefox driver driver = new FirefoxDriver(); driver.manage().window().maximize(); } @Test public void TestLogin() { driver.navigate().to("http://www.webkul.com"); String PageTitle= driver.getTitle(); System.out.println("title is ============="+PageTitle); } @AfterTest public void TearDown() { if(driver!=null) System.out.println("Close Firefox browser"); driver.quit(); } }
Now we will run the above program using TestNG framework, which will first open firefox browser and check ‘webkul Home Page Title’.
OUTPUT CONSOLE-
Execution Of Chrome Driver
Now to run selenium webdriver in Chrome browser, you need to take the help of ‘ChromeDriver’ which selenium webdriver uses to control chrome.Let’s continue it with an example-
package Chrome; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class GoogleDriver { WebDriver driver; @BeforeTest public void Setup() { //Automatically fetches the project path via getProperty method String chrome_path = System.getProperty("user.dir"); //set the property for chrome driver, specify its location via the webdriver.chrome.driver System.setProperty("webdriver.chrome.driver",chrome_path+"/chromedriver"); //Declaring and initializing chrome driver driver = new ChromeDriver(); driver.manage().window().maximize(); } @Test public void Login() { driver.navigate().to("http://www.webkul.com"); String PageTitle= driver.getTitle(); System.out.println("title is ============="+PageTitle); } @AfterTest public void TearDown() { if(driver!=null) System.out.println("Close chrome browser"); driver.quit(); } }
As we are working on chrome browser,we have imported Statement
‘org.openqa.selenium.chrome.ChromeDriver’ which allows us to access classes
from selenium webdriver packages.
Execution Of PhantomJS Driver
PhantomJSDriver-1.0.x.jar can also be downloaded and configured in Eclipse manually.Let’s continue with an example-
package PhantomJS; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class PhantomDriver { WebDriver driver; @BeforeTest public void Setup() { DesiredCapabilities caps = new DesiredCapabilities(); // not really needed: JS enabled by default caps.setJavascriptEnabled(true); //Screen Capture caps.setCapability("takesScreenshot", true); //Declaring and Initializing PhantomJS driver driver = new PhantomJSDriver(caps); } @Test public void GetUrl() throws IOException { driver.navigate().to("http://www.webkul.com"); String PageTitle= driver.getTitle(); // Capture the webpage TakesScreenshot object = (TakesScreenshot)driver; File screen = ((TakesScreenshot) object).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screen,new File("screen.png")); System.out.println("title is ============="+PageTitle); } @AfterTest public void TearDown() { driver.quit(); } }
Now Run the above script. You will observe the output is shown in console
and no browser is launched.
Execution Of Html Unit Driver
HTML Unit Driver is similar to PhantomJS. HtmlUnit driver do not have GUI so you can not see your test execution on your screen.So it is most light weight and fastest headless browser for WebDriver.
Also you only have to add the standard selenium library files to the project. No additional jar files are required. Let’s takes an example-
package htmlUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import org.testng.annotations.Test; public class UnitDriver { WebDriver driver; @Test public void SetUp() throws InterruptedException { //Declaring and initialize HtmlUnitWebDriver WebDriver driver = new HtmlUnitDriver(); //open webkul webpage driver.get("http://www.webkul.com"); //wait for 5 second to load the page Thread.sleep(5000); //Print the title System.out.println("Title of the page "+ driver.getTitle()); } }
When execution completed, You will see result in console.
Be the first to comment.