gpt4 book ai didi

java - 使用 ExtentReports 进行并行 TestNG 测试使用了错误的 @BeforeMethod

转载 作者:行者123 更新时间:2023-11-30 06:51:35 24 4
gpt4 key购买 nike

我正在一台机器上运行 ThreadLocal 测试。我使用 @BeforeMethod 中的代码来启动网页。我还尝试过将其单独编写为我的 Base Test 类中的方法,并在 @BeforeMethod 中调用它。

一切似乎都工作正常,除了错误的用户通过错误的测试登录。这很奇怪,因为 Login 方法不在 BaseTest 类中,它是从页面对象调用的(并且它工作正常,除了登录错误的用户进行测试之外)。

在这个例子中,我将问题代码包含在@BeforeMethod中,并且还注释掉了单独的“initialize”方法和方法调用,因此您可以看到我尝试过的两种方法。

public class StackExample {

protected String baseURL;
public String browser;
private static ThreadLocal<WebDriver> threadedDriver = new ThreadLocal<WebDriver>();

@BeforeMethod(alwaysRun = true)
@Parameters({ "browser", "loginType" })
public void setup(String browser, String loginType, Method caller)
throws MalformedURLException, InterruptedException {
WebDriver driver = null;

// Browsers
if (browser.equalsIgnoreCase("Internet Explorer")) {
System.setProperty("webdriver.ie.driver", "C:\\Users\\automation\\Selenium\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
} else if (browser.equalsIgnoreCase("Firefox")) {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\automation\\Selenium\\geckodriver.exe");
ProfilesIni firProfiles = new ProfilesIni();
FirefoxProfile wbdrverprofile = firProfiles.getProfile("Webdriver2");
driver = new FirefoxDriver(wbdrverprofile);
} else if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\automation\\Selenium\\chromedriver.exe");
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("MicrosoftEdge")) {
System.setProperty("webdriver.edge.driver", "C:\\Users\\automation\\Selenium\\MicrosoftWebDriver.exe");
driver = new EdgeDriver();
}

setWebDriver(driver);
this.browser = browser;
System.out.println(browser);
// initialize(loginType);
System.out.println(loginType);

if (loginType.equalsIgnoreCase("client"))
ClientReportFactory
.getTest(StringUtils.join("Client Test"), ' ') + " ("
+ browser + ")", "This test is located in class: " + getClass().getName());
else if (loginType.equalsIgnoreCase("advisor"))
AdvisorReportFactory
.getTest(StringUtils.join("Advisor Test"), ' ') + " ("
+ browser + ")", "This test is located in class: " + getClass().getName());

if (loginType.equalsIgnoreCase("client"))
baseURL = "ClientWebsite.example";
else if (loginType.equalsIgnoreCase("advisor"))
baseURL = "AdvisorWebsite.example";
else {
System.out.println("Client or Advisor must be specified in TestNG XML");
}
driver.get(baseURL);
driver.manage().window().maximize();

}

// public void initialize(String loginType) throws InterruptedException {
// if (loginType.equalsIgnoreCase("client"))
// baseURL = "ClientWebsite.example";
// else if (loginType.equalsIgnoreCase("advisor"))
// baseURL = "AdvisorWebsite.example";
// else{
// System.out.println("Client or Advisor must be specified in TestNG XML");
// }
// driver.get(baseURL);
// driver.manage().window().maximize();
//
// }

public static WebDriver getDriver() {
return threadedDriver.get();
}

static void setWebDriver(WebDriver driver) {
threadedDriver.set(driver);
}

@AfterMethod // (alwaysRun = true)
@Parameters({ "loginType" })
public void afterMethod(Method caller, String loginType) {
// Here we are making sure we close the same test we opened.
System.out.println(loginType);
if (loginType.equalsIgnoreCase("client"))
ClientReportFactory
.closeTest(StringUtils.join("Client Test"), ' ') + " ("
+ browser + ")");
else if (loginType.equalsIgnoreCase("advisor"))
AdvisorReportFactory
.closeTest(StringUtils.join("Advisor Test"), ' ') + " ("
+ browser + ")");
getDriver().quit();
threadedDriver.set(null);
}

@AfterSuite
@Parameters({ "loginType" })
public void afterSuite(String loginType) {

if (loginType.equalsIgnoreCase("client"))
ClientReportFactory.closeReport();
else if (loginType.equalsIgnoreCase("advisor"))
AdvisorReportFactory.closeReport();

if (getDriver() != null) {
getDriver().quit();
} else {
System.out.println("Drivers already closed");
}
}
}

这是我的测试方法。他们从页面对象调用实际的 SignIn 方法。我知道这不是问题,因为从页面对象调用的所有其他方法都可以通过正确的测试正常工作。

public class StackExampleTests extends StackExample {

@Test(enabled = true, priority = 0)
public void ClientTest1() throws Exception {
ExtentTest t = ClientReportFactory.getTest();
t.log(LogStatus.INFO, "Client 1 Login for " + browser);
try {
Login objLogin = new Login(getDriver());
String username = "username1";
String password = "password1";
t.log(LogStatus.INFO, "Logging in as user: " + username);
objLogin.SignIn(username, password);

// perform First Client's tests here

} catch (Exception e) {
t.log(LogStatus.WARNING, "Exception found: " + e.getMessage().substring(0, 400)
+ t.addBase64ScreenShot(ClientReportFactory.CaptureScreen(getDriver())));
}
}

@Test(enabled = true, priority = 1)
public void ClientTest2 throws Exception{
ExtentTest t = ClientReportFactory.getTest();
t.log(LogStatus.INFO, "Client 2 Login for " + browser);
try {
Login objLogin = new Login(getDriver());
String username = "username2";
String password = "password2";
t.log(LogStatus.INFO, "Logging in as user: " + username);
objLogin.SignIn(username, password);

// perform Second Client's tests here

} catch (Exception e) {
t.log(LogStatus.WARNING, "Exception found: " + e.getMessage().substring(0, 400)
+ t.addBase64ScreenShot(ClientReportFactory.CaptureScreen(getDriver())));
}
}
}

我认为这个跟踪意味着它正在同一个线程中启动我的所有 3 个测试。但我还没有完全做到

Starting ChromeDriver 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9) on port 44694
Only local connections are allowed.

Starting ChromeDriver 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9) on port 12513
Only local connections are allowed.

Starting ChromeDriver 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9) on port 32651
Only local connections are allowed.

最佳答案

看来您的测试遇到了驾驶员身份危机,WebDriver本地并行运行时属于它们。

设置ThreadLocal<WebDriver> threadedDriver , getDriver() ,和setDriver() static应该可以解决问题。

关于java - 使用 ExtentReports 进行并行 TestNG 测试使用了错误的 @BeforeMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42674355/

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