we can run test script in different browsers using web driver. Write one test script and configure in testng xml to run that test case in IE, firefox chrome.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class WebDriverDemo {
private static WebDriver driver = null;
@Test
@Parameters( {"BROWSER"})
public void testread(String BROWSER)throws Exception{
System.out.println("Browser: " + BROWSER);
if (BROWSER.equals("FF")) {
System.out.println("FF is selected");
driver = new FirefoxDriver();
} else if (BROWSER.equals("IE")) {
System.out.println("IE is selected");
driver = new InternetExplorerDriver();
} else if (BROWSER.equals("HU")) {
System.out.println("HU is selected");
driver = new HtmlUnitDriver();
} else if (BROWSER.equals("CH")){
System.out.println("Google chrome is selected");
driver = new ChromeDriver();
}
driver.navigate().to("http://www.yahoo.com");
Thread.sleep(10000);
WebElement search= driver.findElement(By.name("p"));
search.sendKeys("automation blog by niraj");
search.submit();
Thread.sleep(5000);
Assert.assertTrue(driver.getPageSource().contains("automationtricks.blogspot.com"),"Failed in "+ BROWSER);
driver.close();
}
}
In the above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.
TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="webDriver">
<test name="WebDriverDemo Witn FF" preserve-order="true">
<parameter name="BROWSER" value="FF" />
<classes>
<class name="com.web.WebDriverDemo" />
</classes>
</test>
<test name="WebDriverDemo with IE" preserve-order="ture">
<parameter name="BROWSER" value="IE"></parameter>
<classes>
<class name="com.web.WebDriverDemo"></class>
</classes>
</test>
<test name="WebDriverDemo with HTML unit" preserve-order="true">
<parameter name="BROWSER" value="HU"></parameter>
<classes>
<class name="com.web.WebDriverDemo"></class>
</classes>
</test>
<test name="WebDriverDemo with chrome" preserve-order="true">
<parameter name="BROWSER" value="CH"></parameter>
<classes>
<class name="com.web.WebDriverDemo"></class>
</classes>
</test>
</suite>
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class WebDriverDemo {
private static WebDriver driver = null;
@Test
@Parameters( {"BROWSER"})
public void testread(String BROWSER)throws Exception{
System.out.println("Browser: " + BROWSER);
if (BROWSER.equals("FF")) {
System.out.println("FF is selected");
driver = new FirefoxDriver();
} else if (BROWSER.equals("IE")) {
System.out.println("IE is selected");
driver = new InternetExplorerDriver();
} else if (BROWSER.equals("HU")) {
System.out.println("HU is selected");
driver = new HtmlUnitDriver();
} else if (BROWSER.equals("CH")){
System.out.println("Google chrome is selected");
driver = new ChromeDriver();
}
driver.navigate().to("http://www.yahoo.com");
Thread.sleep(10000);
WebElement search= driver.findElement(By.name("p"));
search.sendKeys("automation blog by niraj");
search.submit();
Thread.sleep(5000);
Assert.assertTrue(driver.getPageSource().contains("automationtricks.blogspot.com"),"Failed in "+ BROWSER);
driver.close();
}
}
In the above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.
TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="webDriver">
<test name="WebDriverDemo Witn FF" preserve-order="true">
<parameter name="BROWSER" value="FF" />
<classes>
<class name="com.web.WebDriverDemo" />
</classes>
</test>
<test name="WebDriverDemo with IE" preserve-order="ture">
<parameter name="BROWSER" value="IE"></parameter>
<classes>
<class name="com.web.WebDriverDemo"></class>
</classes>
</test>
<test name="WebDriverDemo with HTML unit" preserve-order="true">
<parameter name="BROWSER" value="HU"></parameter>
<classes>
<class name="com.web.WebDriverDemo"></class>
</classes>
</test>
<test name="WebDriverDemo with chrome" preserve-order="true">
<parameter name="BROWSER" value="CH"></parameter>
<classes>
<class name="com.web.WebDriverDemo"></class>
</classes>
</test>
</suite>
GSSSB 2480 Revenue Talati Bharti Recruitment 2016
ReplyDeleteThanks for the marvelous posting! I certainly enjoyed reading it, you are a great author.....
Assam 2564 Police Constable Recruitment 2016 Apply Online
ReplyDeleteThe information mentioned in the blog are several of the very best available................
Freelance Best Makeup & Hair Artist in Jaipur with huge experience and Specialization in Bridal and Wedding Makeup,Celebrity Makeup,Professional Makeup,Creative Makeup,Bollywood Makeup..
ReplyDeleteFiza Makeup Academy
Fiza Makeup and Hair Artist
Wedding Makeup Artist in jaipur
Bridal Makeup Artist in jaipur
Professional Makeup Artist in jaipur
Hair and Makeup Artist in jaipur
Celebrity Makeup Artist in jaipur
Creative Makeup Artist in jaipur
Bollywood Makeup Artist in jaipur
Character Makeup Artist in jaipur
Fiza Makeup Academy Rajasthan
It is nice post and I found some interesting information on this blog, keep it up. Thanks for sharing.
ReplyDeleteBest Bridal Makeup Artist in Chennai
I am always searching online for articles that can help me. There is obviously a lot to know about this.
ReplyDeleteบาคาร่า
จีคลับ
baccarat
Thank you so much for posting..
ReplyDeleteBest Training Classes for Core Java in Jaipur