我想在所有多个浏览器中运行selenium webdriver测试用例但不能并行。是否可以在不使用xml和selenium grid的情况下在所有多个浏览器中运行测试用例。我们可以通过使用注释和java类。我希望我的测试用例首先在firefox中执行,在firefox中执行完毕后,它应该以chrome开始执行,等等。
我尝试过这段代码,但是使用xml执行并行。
CrossBrowserScript.java
import java.util.concurrent.TimeUnit;
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.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class CrossBrowserScript {
WebDriver driver;
/**
* This function will execute before each Test tag in testng.xml
* @param browser
* @throws Exception
*/
@BeforeTest
@Parameters("browser")
public void setup(String browser) throws Exception{
//Check if parameter passed from TestNG is 'firefox'
if(browser.equalsIgnoreCase("firefox")){
//create firefox instance
driver = new FirefoxDriver();
}
//Check if parameter passed as 'chrome'
else if(browser.equalsIgnoreCase("chrome")){
//set path to chromedriver.exe
System.setProperty("webdriver.chrome.driver",".\\chromedriver.exe");
//create chrome instance
driver = new ChromeDriver();
}
//Check if parameter passed as 'IE'
else if(browser.equalsIgnoreCase("ie")){
//set path to IE.exe
System.setProperty("webdriver.ie.driver",".\\IEDriverServer.exe");
//create IE instance
driver = new InternetExplorerDriver();
}
else{
//If no browser passed throw exception
throw new Exception("Browser is not correct");
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void testParameterWithXML() throws InterruptedException{
driver.get("http://demo.guru99.com/V4/");
//Find user name
WebElement userName = driver.findElement(By.name("uid"));
//Fill user name
userName.sendKeys("guru99");
//Find password
WebElement password = driver.findElement(By.name("password"));
//Fill password
password.sendKeys("guru99");
}
}
testngCrossBrowser.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="3" parallel="tests" >
<test name="ChromeTest">
<parameter name="browser" value="Chrome" />
<classes>
<class name="parallelTest.CrossBrowserScript">
</class>
</classes>
</test>
<test name="FirefoxTest">
<parameter name="browser" value="Firefox" />
<classes>
<class name="parallelTest.CrossBrowserScript">
</class>
</classes>
</test>
<test name="IETest">
<parameter name="browser" value="IE" />
<classes>
<class name="parallelTest.CrossBrowserScript">
</class>
</classes>
</test>
</suite>
答案 0 :(得分:2)
使用JUnit,您可以创建MethodRule(http://junit.org/apidocs/org/junit/rules/MethodRule.html),它将在所有浏览器中运行测试。
示例:
public class ManyBrowsers implements MethodRule {
public static WebDriver driver;
@Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
//RUN FIREFOX
driver = new FirefoxDriver();
base.evaluate();
driver.quit();
//RUN CHROME
File f = //PATH to CHROME DRIVER
System.setProperty("webdriver.chrome.driver", f.getAbsolutePath());
driver = new ChromeDriver();
base.evaluate();
driver.quit();
}
};
}
}
示例测试:
public class VisitGoogle {
@Rule
public ManyBrowsers browsers = new ManyBrowsers();
@Test
public void test() {
ManyBrowsers.driver.navigate().to("https://www.google.com/");
}
}
答案 1 :(得分:0)
尝试将thread-count="3"
更改为thread-count="1"
。这应该按照 TestNG 文件中提到的顺序执行测试。