我正在尝试在硒GRID中运行以下代码(执行“本地”代码,可以正常工作),遵循代码段:
SuiteTest.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SuiteTest" verbose="1" thread-count="2" parallel="tests">
<test name="FIREFOX Test">
<parameter name="browser" value="FIREFOX" />
<classes>
<class name="test.CN01_CT01_IncluirConteudoMP3" />
<class name="test.CN01_CT02_IncluirConteudoMP4" />
</classes>
</test>
<test name="CHROME Test">
<parameter name="browser" value="CHROME" />
<classes>
<class name="test.CN01_CT01_IncluirConteudoMP3" />
<class name="test.CN01_CT02_IncluirConteudoMP4" />
</classes>
</test>
</suite>
BaseTest:
public class BaseTest {
protected static ExtentReports extent;
public static String testName;
@Parameters({ "browser" })
@BeforeMethod
public void BeforeTest(Method method, Browsers browser) throws IOException, BiffException {
Propriedades.browser = browser;
String browserStr = browser.toString();
System.out.println("Before ........" + method.getName());
//Recuperar nome do Teste
testName = method.getName() + "-" + browserStr;
// Realizando Login
LoginPage.realizarLogin();
//Iniciar o relatorio
extent = ExtentReport.StartReport(testName);
}
@AfterMethod
public void finaliza(ITestResult result) throws IOException {
System.out.println("After ........");
//Validar Resultado dos Testes
ExtentReport.ResultTest(result, testName);
// Realizar Logout
LoginPage.realizarLogout();
// Fechando Browser
if (Propriedades.FECHAR_BROWSER) {
killDriver();
}
}
@AfterSuite
public static void finalizaTudo(){
DriverFactory.killDriver();
extent.flush();
System.out.println("AfterSuite........");
System.out.println(ExtentReport.diretorioReport);
//DriverFactory.executeChrome(ExtentReport.diretorioReport);
}
}
BasePage:
public class BasePage {
protected static DSL dsl;
static List<String> result = Ambiente.dadosAmbiente();
static String url = result.get(0);
protected static String user = result.get(1);
protected static String pass = result.get(2);
public BasePage() {
dsl = new DSL();
}
}
DSL:
public class DSL {
public void clicarRadio(By by) {
getDriver().findElement(by).click();
}
Other methods to access the screen elements (Text, Click, Button, Radio, Combo and etc.)
(...)
}
DriveFactory:
public class DriverFactory {
static WebDriver driver;
private DriverFactory() {
}
public static WebDriver getDriver() {
if(Propriedades.tipoExecucao == TipoExecucao.LOCAL){
if (driver == null) {
switch (Propriedades.browser) {
case CHROME:
executeChrome(BasePage.url);
break;
case FIREFOX:
executeFirefox(BasePage.url);
break;
case IE:
executeIE(BasePage.url);
break;
case EDGE:
executeEgde(BasePage.url);
break;
}
driver.manage().window().maximize();
}
}
if(Propriedades.tipoExecucao == TipoExecucao.GRID) {
DesiredCapabilities cap = null;
switch (Propriedades.browser) {
case CHROME:
cap = DesiredCapabilities.chrome();
break;
case FIREFOX:
cap = DesiredCapabilities.firefox();
break;
case IE:
cap = DesiredCapabilities.internetExplorer();
break;
case EDGE:
cap = DesiredCapabilities.edge();
break;
}
try {
driver = new RemoteWebDriver(new URL("http://192.168.0.72:4444/wd/hub"), cap);
} catch (MalformedURLException e) {
System.err.println("Falha ao conectar ao GRID");
}
driver.manage().window().maximize();
driver.get(BasePage.url);
System.out.println("Abriu Browser Grid");
}
return driver;
}
public static void killDriver() {
if (driver != null) {
driver.close();
driver.quit();
driver = null;
}
}
}
测试-类:
测试类,仅用于检查执行是否正在登录和注销。
@Test
public void CN01_CT02_IncluirConteudoMp4(){
assertEquals("123", "123");
}
@Test
public void CN01_CT01_IncluirConteudoMp3() throws InterruptedException, AWTException{
assertEquals("123", "123456");
}
配置网格
Config(Json)-集线器:
{
"port": 4444,
"newSessionWaitTimeout": -1,
"servlets" : [],
"withoutServlets": [],
"custom": {},
"capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
"registry": "org.openqa.grid.internal.DefaultGridRegistry",
"throwOnCapabilityNotPresent": true,
"cleanUpCycle": 5000,
"role": "hub",
"debug": false,
"browserTimeout": 0,
"timeout": 1800
}
NodeChrome:
{
"capabilities": [
{
"browserName": "chrome",
"maxInstances": 1,
"webdriver.chrome.driver": "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
}
],
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 1,
"port": 5555,
"register": true,
"registerCycle": 5000,
"hub": "http://192.168.0.72:4444",
"nodeStatusCheckTimeout": 5000,
"nodePolling": 5000,
"role": "node",
"unregisterIfStillDownAfter": 60000,
"downPollingLimit": 1,
"debug": false,
"servlets": [
],
"withoutServlets": [
],
"custom": {
}
}
NodeFirefox:
{
"capabilities": [
{
"browserName": "firefox",
"maxInstances": 1,
"webdriver.gecko.driver": "C:/Program Files/Mozilla Firefox/firefox.exe"
}
],
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 1,
"port": 5556,
"register": true,
"registerCycle": 5000,
"hub": "http://192.168.0.72:4444",
"nodeStatusCheckTimeout": 5000,
"nodePolling": 5000,
"role": "node",
"unregisterIfStillDownAfter": 60000,
"downPollingLimit": 1,
"debug": false,
"servlets": [
],
"withoutServlets": [
],
"custom": {
}
}
使用Selenium GRID执行代码时,系统会打开两个浏览器,但没有登录,就会出现问题(显然是同步问题)。
示例:
登录字段填写“两次”,并返回“无效登录”。
代码是使用Eclipse IDE执行的。 运行(SuiteTest.xml)-使用TestNG运行
答案 0 :(得分:1)
The problem lies in your test code. Your DriverFactory
is initialising a static data member in driver
. Now when you run your tests in parallel via your suite xml file, your tests are really competing with each other and end up overwriting the static data member. You need to refactor your test code. There's no problem with either the Selenium Grid or TestNG for that matter here.