gpt4 book ai didi

java - 使用 TestNG 进行并行测试 - 测试仅在一种浏览器上运行

转载 作者:行者123 更新时间:2023-12-01 10:28:05 25 4
gpt4 key购买 nike

我使用创建了一个测试套件数据提供者数据工厂我的 TestNG 文件正在将浏览器详细信息作为参数发送。在 testNG XML 中,我调用我的数据工厂类。我还使用 browserstack 进行测试(尽管我怀疑这与我遇到的问题有任何关系)

当我不将 parrellel="true"添加到 testng 文件时,测试运行没有任何问题。

我有一种感觉,这与每个浏览器使用相同的驱动程序有关,但目前我还无法深入解决这个问题。

感谢任何指导。

这是代码。

TestNG.XML

<suite name="Suite" verbose="1" parallel="tests">
<!-- Test with Chrome -->
<test name="ChromeTest" group-by-instances="true">
<parameter name="browser" value="Chrome"></parameter>
<parameter name="browserVersion" value="47"></parameter>
<parameter name="platform" value="Windows"></parameter>
<parameter name="platformVersion" value="7"></parameter>
<classes>
<class name="Resources.TestFactory"/>
</classes>
</test>

<!-- Test with Firefox -->
<test name="FirefoxTest" group-by-instances="true">
<parameter name="browser" value="Firefox"></parameter>
<parameter name="browserVersion" value="43"></parameter>
<parameter name="platform" value="Windows"></parameter>
<parameter name="platformVersion" value="7"></parameter>
<classes>
<class name="Resources.TestFactory"/>
</classes>
</test>

</suite>

数据工厂类

public class TestFactory {
@Factory(dataProvider = "LoginCredentials", dataProviderClass=TestData.class)
public Object[] createInstances(int testNo, String userName, String password) {
Object[] result = new Object[1];
int i=0;
System.out.println("Inside LoginCredentials Factory - " + userName + "---" + password);

if(testNo==1){
result[i] = new Test_BookingEngine_Login(userName, password);
i++;
System.out.println("Object Array : " + Arrays.deepToString(result));
}
else if(testNo==2){
result[i] = new Test_BookingManagement_OpenBooking(userName);
i++;
System.out.println("Object Array : " + Arrays.deepToString(result));
}
System.out.println("outside for");
return result;
}
}

套件 - 驱动程序初始化

@BeforeTest
@Parameters(value ={"browser", "browserVersion", "platform", "platformVersion"})
public void initBrowser(String browser, String browserVersion, String platform, String platformVersion) throws Exception{

//Initializing browser in cloud
cloudCaps = new DesiredCapabilities();
cloudCaps.setCapability("browser", browser);
cloudCaps.setCapability("browser_version", browserVersion);
cloudCaps.setCapability("os", platform);
cloudCaps.setCapability("os_version", platformVersion);
cloudCaps.setCapability("browserstack.debug", "true");
cloudCaps.setCapability("browserstack.local", "true");

driver = new RemoteWebDriver(new URL(URL), cloudCaps);

}

示例测试

 public Test_BookingEngine_Login(String userName, String password) {  
this.userName = userName;
this.password = password;
}

@Test (groups = {"Login"})
public void testHomePageAppearCorrect() throws InterruptedException{

//Starting test and assigning test category
test = logger.startTest("Login to Temptation", "<b>Successful user login or Pop up advising incorrect login details</b><br/><br/>" + browserInfo)
.assignCategory("Regression", "Booking Engine")
.assignAuthor("Dinesh Cooray");

System.out.println("Inside login test");
System.out.println("Browser inside login test : ");

driver.get("http://dev-thor2.tempoholidays.com/");
test.log(LogStatus.INFO, "HTML", "Navigated to http://dev-thor2.tempoholidays.com/");

//create Login Page object
objLogin = new BookingEngine_Login(driver);

//login to application
objLogin.loginToTemptationBookingEngine(userName, password, test);

//check if alert advising username or password is is incorrect
try {
//incorrect login details, user should not allow login
if(driver.switchTo().alert().getText().toLowerCase().contains("user name or password is wrong")){
test.log(LogStatus.INFO, "HTML", "<b>Popup - </b>" + driver.switchTo().alert().getText());
driver.switchTo().alert().accept();
Assert.assertTrue(true);
}
}

最佳答案

我猜测 RemoteWebDriver driver; 将是您在类级别添加的一行。发生的情况是您已经在类级别声明了该变量。即内存已经分配给它。当您执行类似 driver = new RemoteWebDriver(new URL(URL), cloudCaps); 的操作时,您只是在每个 @ 中设置和重置同一变量的值测试前

您需要做的是创建一个工厂,它将根据您传递给它的参数返回 RemoteWebDriver 的实例。本质上,工厂将创建一个新对象,并且仅在现有对象不存在时才返回。在 @Test 方法中声明并初始化驱动程序(从工厂)

工厂的示例代码类似于

static RemoteWebDriver firefoxDriver;
static RemoteWebDriver someOtherDriver;


static synchronized RemoteWebDriver getDriver(String browser, String browserVersion, String platform, String platformVersion)
{
if (browser == 'firefox')
{
if (firefoxDriver == null)
{
DesiredCapabilities cloudCaps = new DesiredCapabilities();
cloudCaps.setCapability("browser", browser);
cloudCaps.setCapability("browser_version", browserVersion);
cloudCaps.setCapability("os", platform);
cloudCaps.setCapability("os_version", platformVersion);
cloudCaps.setCapability("browserstack.debug", "true");
cloudCaps.setCapability("browserstack.local", "true");

firefoxDriver = new RemoteWebDriver(new URL(URL),cloudCaps);
}
}
else
{
if (someOtherDriver == null)
{
DesiredCapabilities cloudCaps = new DesiredCapabilities();
cloudCaps.setCapability("browser", browser);
cloudCaps.setCapability("browser_version", browserVersion);
cloudCaps.setCapability("os", platform);
cloudCaps.setCapability("os_version", platformVersion);
cloudCaps.setCapability("browserstack.debug", "true");
cloudCaps.setCapability("browserstack.local", "true");

someOtherDriver = new RemoteWebDriver(new URL(URL),cloudCaps);
}
return someOtherDriver;
}
}
}

关于java - 使用 TestNG 进行并行测试 - 测试仅在一种浏览器上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35249565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com