gpt4 book ai didi

java - 如何将当前功能传递给单个测试

转载 作者:行者123 更新时间:2023-11-29 04:43:52 26 4
gpt4 key购买 nike

这可能就像从另一个类中获取变量一样简单。我仍在学习 Java 和 Selenium。

我希望测试运行报告 (ExtentReports) 能够在 @Test 级别(功能)报告浏览器。目前 Grid 在不同的浏览器上运行相同的测试,报告没有区分它们。

使用 Selenium Grid,我使用 @BeforeMethod 定义了我的 @Test 功能(包括浏览器)。我在我的 BaseTest 类中这样做。

public class BaseTest {
@BeforeMethod(alwaysRun = true)
@Parameters({ "platform", "browser", "version" })
public void setup(String platform, String browser, String version)
throws MalformedURLException, InterruptedException {
RemoteWebDriver driver = null;
//important: Thread local!
threadedDriver = new ThreadLocal<RemoteWebDriver>();
DesiredCapabilities caps = new DesiredCapabilities();
// Platforms
if (platform.equalsIgnoreCase("windows"))
caps.setPlatform(Platform.WINDOWS);
if (platform.equalsIgnoreCase("XP"))
caps.setPlatform(Platform.XP);
if (platform.equalsIgnoreCase("WIN8"))
caps.setPlatform(Platform.WIN8);
if (platform.equalsIgnoreCase("WIN8_1"))
caps.setPlatform(Platform.WIN8_1);
if (platform.equalsIgnoreCase("ANY"))
caps.setPlatform(Platform.ANY);
if (platform.equalsIgnoreCase("MAC"))
caps.setPlatform(Platform.MAC);
if (platform.equalsIgnoreCase("Android"))
caps.setPlatform(Platform.ANDROID);
// Browsers
if (browser.equalsIgnoreCase("Internet Explorer"))
caps.setBrowserName("internet explorer");
if (browser.equalsIgnoreCase("Firefox"))
caps.setBrowserName("firefox");
if (browser.equalsIgnoreCase("chrome"))
caps.setBrowserName("chrome");
if (browser.equalsIgnoreCase("MicrosoftEdge"))
caps.setBrowserName("MicrosoftEdge");
if (browser.equalsIgnoreCase("iPad"))
caps.setBrowserName("ipad");
if (browser.equalsIgnoreCase("iPhone"))
caps.setBrowserName("iphone");
if (browser.equalsIgnoreCase("Android"))
caps.setBrowserName("android");
// Version
caps.setVersion(version);
System.out.println(caps);
System.out.println(browser);
//Initialize driver with capabilities
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);

//this uses below methods to set above RemoteWebDriver to the getDriver()
//method in a threaded instance.
setWebDriver(driver);
initialize();

}
}

所以现在我有一个 browser 本地变量,用于每个线程化网格测试运行。我需要将该变量放入每个 @Test 方法中。这是我在单独类(class)中的@Test。在 try 语句的开头,我想打印 browser 变量当前线程网格测试功能

public class Workflow1 extends BaseTest {
@Test
public void Workflow1TestInvalidPolicyNumbers() throws InterruptedException {
HomePage homePage = new HomePage(getDriver());
ExtentTest testReporter = ComplexReportFactory.getTest();
try {
System.out.println("This is the browser:" + ??(help here)??);
loginMethod("TestUser","TestPassword");
homePage.setFindAPersonOrPolicySearchField("1234");
homePage.clickSearchButton();
testReporter.log(LogStatus.INFO, "Searched \"1234\"");
Thread.sleep(2000);
if (getDriver().getPageSource().contains("Policy numbers should be 7 or 10 digits long"))
testReporter.log(LogStatus.PASS, "Policy numbers should be 7 or 10 digits long");
else
testReporter.log(LogStatus.FAIL, "Results incorrect" + testReporter.addScreenCapture(ComplexReportFactory.CaptureScreen(getDriver())));

} catch (Exception e) {
testReporter.log(LogStatus.ERROR, "Exception found: " + e.getMessage()
+ testReporter.addScreenCapture(ComplexReportFactory.CaptureScreen(getDriver())));
System.out.println(e);
}
}

最佳答案

看起来浏览器信息是在setup()方法中传递给BaseTest.java类的。

您可以将此数据存储在一个变量中,该变量随后可用于所有依赖类:

public class BaseTest {

protected String browser; // add a property to hold this value

@BeforeMethod(alwaysRun = true)
@Parameters({ "platform", "browser", "version" })
public void setup(String platform, String browser, String version)
throws MalformedURLException, InterruptedException {
this.browser = browser; // store the given browser string
RemoteWebDriver driver = null;
//important: Thread local!
threadedDriver = new ThreadLocal<RemoteWebDriver>();
DesiredCapabilities caps = new DesiredCapabilities();
// Platforms
if (platform.equalsIgnoreCase("windows"))
caps.setPlatform(Platform.WINDOWS);

然后 BaseTest 子类可以直接引用它:

public class Workflow1 extends BaseTest {
@Test
public void Workflow1TestInvalidPolicyNumbers() throws InterruptedException {
HomePage homePage = new HomePage(getDriver());
ExtentTest testReporter = ComplexReportFactory.getTest();
try {
System.out.println("This is the browser:" + browser); // then retrieve it

关于java - 如何将当前功能传递给单个测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38058536/

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