gpt4 book ai didi

java - 如何使用 webdriver 和 TestNG 在单个类文件中运行两个 @Test 方法

转载 作者:太空宇宙 更新时间:2023-11-04 11:42:32 26 4
gpt4 key购买 nike

我在Web Driver中有一段代码,如下所述。单个类中有两个 @Test 方法,我尝试使用 testng.xml 并行执行它们。

谁能告诉我我在这里做错了什么

public class CrossBrowserScript {

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 You may need to download it from http://code.google.com/p/selenium/wiki/ChromeDriver

System.setProperty("webdriver.chrome.driver","E:\\Maven\\chromedriver.exe");

//create chrome instance

driver = new ChromeDriver();

}

else if(browser.equalsIgnoreCase("ie")){

//set path to IEdriver.exe You may need to download it from

// 32 bits http://selenium-release.storage.googleapis.com/2.42/IEDriverServer_Win32_2.42.0.zip

// 64 bits http://selenium-release.storage.googleapis.com/2.42/IEDriverServer_x64_2.42.0.zip

System.setProperty("webdriver.ie.driver","E:\\Maven\\IEDriverServer.exe");

//create chrome 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);

}
}

package checking;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class test extends CrossBrowserScript{


@Parameters({ "browser" })
@Test
public void checking_value(){

//WebDriver driver = new FirefoxDriver();
driver.get("http://www.sourcefuse.com/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

if(driver.getPageSource().contains("Our Work"))
{
System.out.println("Found the text");
}

else
{
System.out.println("Not able to found the text");
}

}

@Parameters({ "browser" })
@Test
public void second_method(){


driver.get("http://www.sourcefuse.com/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

if(driver.getPageSource().contains("Our Work"))
{
System.out.println("Found the text");
}

else
{
System.out.println("Not able to found the text");
}

}

}

*************************************************************
TestNG.xml File
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Multiple Tests" parallel="tests" thread-count="2">

<test name="Test One">

<parameter name="browser" value="Chrome" />
<classes>
<class name="checking.test">

</class>

</classes>
</test>




<test name="Test two">

<parameter name="browser" value="Firefox" />
<classes>
<class name="checking.test">
</class>

</classes>
</test>

</suite>

当我执行时,它打开 2 个浏览器并执行上述代码总共 4 次,但我担心的是我想在 2 个不同的浏览器中运行两个 @Test

最佳答案

实现此目的的一种方法是将您的 2 个方法粘贴到 2 个不同的类中,它们都扩展您的 CrossBrowserScript(即 testChrome 和 testFirefox)

在您的 xml 中尝试一下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Multiple Tests" parallel="tests" thread-count="2">

<test name="Test One">

<parameter name="browser" value="Chrome" />
<classes>
<class name="checking.testChrome">

</class>

</classes>
</test>




<test name="Test two">

<parameter name="browser" value="Firefox" />
<classes>
<class name="checking.testFirefox">
</class>

</classes>
</test>

</suite>

关于java - 如何使用 webdriver 和 TestNG 在单个类文件中运行两个 @Test 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42647787/

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