I added the preserve-order parameter in the suite. The tests run in the order we specified.<suite name="Suite" preserve-order="true">
2. I want to run the tests in multiple browser ? show me with code and xml file.
Ans:
Multi Browser Testing using Selenium TestNG
This Section describes how to run your Test cases on different browsers.
Few Simple steps Using TestNG :)Step 1: Create your Script. Using TestNG annotations. Define parameters (using @Parameters) for taking input value i.e, which browser should be used for Running the Test
Step 2: Create a TestNG XML for running your script
Step 3: Configure the TestNG XML for passing parameters i.e, to tell which browser should be used for Running the Test
Step 4: Run the TestNG XML which can pass the appropriate browser name to the Script such that the Test Case is executed in a specified browser
Programatically this can be Done as follows:
Step 1:
package atu.multibrowser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MultiBrowserTest {
private WebDriver driver;
// Configure for multi browser drivers
@Parameters("browser")
@BeforeClass
public void beforeTest(String browser) {
if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("chrome")) {
// Set Path for the executable file
System.setProperty("webdriver.chrome.driver",
"D:\\chromedriver.exe");
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("ie")) {
// Set Path for the executable file
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
} else {
throw new IllegalArgumentException("The Browser Type is Undefined");
}
// Open App
driver.get("http://demo.opensourcecms.com/wordpress/wp-login.php");
}
@Test
public void login() throws InterruptedException {
// Enter UserName
driver.findElement(By.id("user_login")).clear();
driver.findElement(By.id("user_login")).sendKeys("admin");
// Enter Password
driver.findElement(By.id("user_pass")).clear();
driver.findElement(By.id("user_pass")).sendKeys("demo123");
// Click on Submit button
driver.findElement(By.id("wp-submit")).submit();
}
@AfterClass
public void afterTest() {
try {
driver.quit();
} catch (Exception e) {
driver = null;
}
}
}
Step 2 & Step 3: The below XML is configured to run the Test Case in Firefox, Chrome and IE browser in sequential manner
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="FirefoxTest">
<parameter name="browser" value="firefox" />
<classes>
<class name="atu.multibrowser.MultiBrowserTest" />
</classes>
</test>
<test name="ChromeTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="atu.multibrowser.MultiBrowserTest" />
</classes>
</test>
<test name="IETest">
<parameter name="browser" value="ie" />
<classes>
<class name="atu.multibrowser.MultiBrowserTest" />
</classes>
</test>
</suite>