Selenium webdriver 为什么我的测试在使用TestNG和Grid 2进行并行测试时失败?
各位,希望你们能帮助我,提前谢谢。我用Webdriver和TestNG创建了10个测试,用Grid2运行。当启动1个节点时,所有测试都通过。当启动2个节点时,它可以并行运行。但有些测试失败了。错误如下:Selenium webdriver 为什么我的测试在使用TestNG和Grid 2进行并行测试时失败?,selenium-webdriver,testng,selenium-grid,Selenium Webdriver,Testng,Selenium Grid,各位,希望你们能帮助我,提前谢谢。我用Webdriver和TestNG创建了10个测试,用Grid2运行。当启动1个节点时,所有测试都通过。当启动2个节点时,它可以并行运行。但有些测试失败了。错误如下: org.openqa.selenium.WebDriverException:未知错误:无法聚焦元素 我的步骤: 1.启动集线器 D:\Selenium>java -jar selenium-server-standalone-2.35.0.jar -role hub 2.Start No
org.openqa.selenium.WebDriverException:未知错误:无法聚焦元素
我的步骤:
1.启动集线器
D:\Selenium>java -jar selenium-server-standalone-2.35.0.jar -role hub
2.Start Node 1
D:\Selenium>java -jar selenium-server-standalone-2.35.0.jar -role wd -port 5556 -hub ip/grid/register -Dwebdriver.chrome.driver=D:\Selenium\chromedriver.exe -browser "browserName=chrome,maxInstances=1,platform=WINDOWS"
3.启动节点2
C:\Selenium>java -jar selenium-server-standalone-2.35.0.jar -role wd -hub ip/grid/register -Dwebdriver.chrome.driver=C:\Selenium\chromedriver.exe -browser "browserName=chrome,maxInstances=1,platform=WINDOWS"
4.然后运行测试
testng.xml
================================================================
测试等级2
package com.ms.gridDemo.test;
import org.testng.annotations.Test;
import com.ms.gridDemo.test.AbstractTest;
public class LoginTest2 extends AbstractTest {
@Test
public void test6() throws InterruptedException {
login();
}
@Test
public void test7() throws InterruptedException {
login();
}
@Test
public void test8() throws InterruptedException {
login();
}
@Test
public void test9() throws InterruptedException {
login();
}
@Test
public void test10() throws InterruptedException {
login();
}
}
您的驱动程序对象在您的类中。当每个测试运行时,相同的对象将被初始化。当您按顺序运行时,这没关系,因为在某个时间点上只使用了一个驱动程序。但是在并行方法运行的情况下,一旦第二个测试开始,第一个驱动程序引用就会丢失,您的第一个测试也会开始查看第二个测试的驱动程序引用。尝试探索Threadlocal以进行并行运行。您的驱动程序对象在您的类中。当每个测试运行时,相同的对象将被初始化。当您按顺序运行时,这没关系,因为在某个时间点上只使用了一个驱动程序。但是在并行方法运行的情况下,一旦第二个测试开始,第一个驱动程序引用就会丢失,您的第一个测试也会开始查看第二个测试的驱动程序引用。尝试探索Threadlocal以进行并行运行
package com.ms.gridDemo.test;
import java.net.MalformedURLException;
import java.net.URL;
import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import com.google.common.base.Function;
public class AbstractTest {
private WebDriver driver = null;
private String baseURL;
public void login() throws InterruptedException {
driver.get(baseURL);
waitForElementVisible(driver,By.id("email"));
WebElement emailElem = driver.findElement(By.id("email"));
WebElement passwordElem = driver.findElement(By.id("password"));
emailElem.clear();
emailElem.sendKeys("jeff.liang@ms.com");
passwordElem.clear();
passwordElem.sendKeys("!Q@W3e4r");
WebElement submitEl = driver.findElement(By.id("submit_btn"));
submitEl.click();
Thread.sleep(2000);
Assert.assertFalse("Login failed.",IsTextPresent(driver,"The Email address or password you entered is incorrect."));
;
}
@BeforeMethod
public void beforeMethod() {
DesiredCapabilities dc = DesiredCapabilities.chrome();
URL url = null;
try {
url = new URL("http://localhost:4444/wd/hub");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver =new RemoteWebDriver(url, dc);
driver.manage().window().maximize();
baseURL ="https://mercury-uat.morningstar.com";
}
@AfterMethod
public void afterMethod() {
driver.quit();
}
public static WebElement waitForElementVisible(WebDriver driver, final By locator) {
Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
try {
WebElement el = driver.findElement(locator);
if (el.isDisplayed()) {
return el;
}
} catch (Exception e) {
}
return null;
}
};
WebDriverWait wait = new WebDriverWait(driver,120,300);
return wait.until(waitFn);
}
public boolean IsTextPresent(WebDriver driver, String text) {
try {
return driver.getPageSource().contains(text);
} catch (Exception e) {
return false;
}
}
}
package com.ms.gridDemo.test;
import org.testng.annotations.Test;
import com.ms.gridDemo.test.AbstractTest;
public class LoginTest extends AbstractTest {
@Test
public void test1() throws InterruptedException {
login();
}
@Test
public void test2() throws InterruptedException {
login();
}
@Test
public void test3() throws InterruptedException {
login();
}
@Test
public void test4() throws InterruptedException {
login();
}
@Test
public void test5() throws InterruptedException {
login();
}
}
package com.ms.gridDemo.test;
import org.testng.annotations.Test;
import com.ms.gridDemo.test.AbstractTest;
public class LoginTest2 extends AbstractTest {
@Test
public void test6() throws InterruptedException {
login();
}
@Test
public void test7() throws InterruptedException {
login();
}
@Test
public void test8() throws InterruptedException {
login();
}
@Test
public void test9() throws InterruptedException {
login();
}
@Test
public void test10() throws InterruptedException {
login();
}
}