XML代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
<test thread-count="5" name="Test">
<parameter name="browser" value="firefox">
<classes>
<class name="Crossbrowser.Test1"/>
</classes>
</parameter>
<test thread-count="5" name="chromeTest">
<parameter name="browser" value="chrome">
<classes>
<class name="Crossbrowser.Test1"/>
</classes>
</parameter>
</test> <!-- Test -->
</test> <!-- Test -->
</suite> <!-- Suite -->
Java代码:
package Crossbrowser;
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.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Test1 {
WebDriver driver;
@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", "F:\\New folder\\chromedriver.exe");
//create chrome instance
driver = new ChromeDriver();
}
//Check if parameter passed as 'Edge'
else if(browser.equalsIgnoreCase("Edge")){
//set path to Edge.exe
System.setProperty("webdriver.edge.driver","F:\\New folder (2)\\MicrosoftWebDriver.exe");
//create Edge instance
driver = new EdgeDriver();
}
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("xxxxxxxxxxxxx");
driver.manage().window().maximize();
driver.findElement(By.xpath("/html[1]/body[1]/div[2]/header[1]/ss-header[1]/div[1]/div[1]/div[1]/span[1]/a[1]")).click();
Thread.sleep(2000);
//Find user name
WebElement userName = driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[1]/div[1]/ss-auth-form[1]/md-card[1]/ss-login-form[1]/div[1]/form[1]/input[1]"));
//Fill user name
userName.sendKeys("xxxxxxxxxxxxx");
//Find password
WebElement password = driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[1]/div[1]/ss-auth-form[1]/md-card[1]/ss-login-form[1]/div[1]/form[1]/input[2]"));
//Fill password
password.sendKeys("1234567890");
Thread.sleep(2000);
driver.findElement(By.xpath("//form[@name='myForm']//ss-submit-button[@label='Log In']//input[@class='submit']")).click();
}
}
虽然作为Single Suite执行,但下面的代码运行良好,但是在并行模式下执行相同操作时,出现错误:
元素类型“ test”的内容必须匹配 “((方法选择器?,参数*,组?,包?,类?)”。
为什么会出现此错误?
PS:在单独的Web浏览器中执行时,没有出现此错误。我只有在开始以并行模式运行时才能得到它。
答案 0 :(得分:0)
在套件级别指定线程计数,并如下更改您的testng xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="5">
<test name="chromeTest">
<parameter name="browser" value="chrome">
<classes>
<class name="Crossbrowser.Test1"/>
</classes>
</parameter>
</test> <!-- Test -->
</suite>
答案 1 :(得分:0)
请尝试遵循XML。这对我有用。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="5" >
<test name="FirefoxTest">
<parameter name="browser" value="firefox" />
<classes>
<class name="Crossbrowser.Test1" />
</classes>
</test>
<test name="chromeTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="Crossbrowser.Test1" />
</classes>
</test>
</suite>