【问题标题】:Test fails when @AfterTest annotation is changed to @Test@AfterTest 注解更改为 @Test 时测试失败
【发布时间】:2020-06-01 14:26:21
【问题描述】:

我有以下类文件

public class AuthenticationTest extends Base {
    private static Logger log = LogManager.getLogger(AuthenticationTest.class.getName());
    private static WebDriver driver;
    LoginPageFactory lp;

    @BeforeTest
    public void initialize() {
        //ExtentReport.config(LoginTest.class.getName());
        log.debug("Initializing WebDriver");
        driver = getDriver();   
        log.debug("Driver is Initialized");
        driver.get(p.getProperty("url"));
        log.debug("Navigated to Login page");

    }


    @Test
    public void login() {               

                try {       

                Map<String,String> data = ExcelUtil.getData("R1SanityTestData.xlsx", "LoginTest", "ValidLoginTest");

                lp = new LoginPageFactory(driver);
                log.debug("LoginPageFactory class initialized");

                lp.txtUsername().click();
                lp.txtUsername().clear();
                lp.txtUsername().sendKeys(data.get("UserName"));
                log.debug("Username entered");
                lp.txtPassword().clear();
                lp.txtPassword().sendKeys(data.get("Password"));
                log.debug("Password entered");
                lp.btnLogin().click();
                log.debug("Login button clicked");  

                LandingPageFactory lpf = new LandingPageFactory(driver);    

                Assert.assertEquals(lpf.lblAppName().getText(), "OD App");
                Assert.assertTrue(lpf.iconProfile().isDisplayed());
                log.info("Login Success");
                String path = Screenshot.takeScreenshot(driver, "LoginTest/LoginStatus");
                }
                catch(IOException e) {
                    log.fatal("IOException in LoginTest");
                    log.fatal(e.getMessage() +" : " + e.getStackTrace());
                    driver.quit();
                }
                catch(NoSuchElementException e){
                    log.fatal("NosuchElementException in LoginTest");
                    log.fatal(e.getMessage() +" : " + e.getStackTrace());
                    driver.quit();
                }

                catch(Exception e) {
                    log.fatal("Exception in LoginTest");
                    log.fatal(e.getMessage() +" : " + e.getStackTrace());
                    driver.quit();
                }

    }               

    @AfterTest
    public void logOut() {
        CommonPageFactory cpf = new CommonPageFactory(driver);
        cpf.linkViewProfile().click();      
        log.debug("View Profile Clicked");  
        cpf.linkLogOut().click();       
        log.debug("Log Out Clicked");
        driver.quit();
    }   
}

当我单独运行这个类(运行 AS --> TestNG 测试)时,上面的测试工作正常。但是,当我将 logOut() 方法的注释更改为 @Test 时,登录方法通过但需要花费大量时间来执行,并且 logOut 测试失败,第一个元素出现 org.openqa.selenium.NoSuchElementException。

控制台输出: [RemoteTestNG] 检测到TestNG 7.0.1版本 在端口 42250 上启动 ChromeDriver 83.0.4103.39 (ccbf011cb2d2b19b506d833300373761342c20cd-refs/branch-heads/4103@{#416}) 只允许本地连接。 请参阅https://chromedriver.chromium.org/security-considerations 获取有关确保 ChromeDriver 安全的建议。 ChromeDriver 已成功启动。 2020 年 5 月 31 日晚上 8:14:54 org.openqa.selenium.remote.ProtocolHandshake createSession 信息:检测到的方言:W3C 通过:登录 失败:注销 org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{"method":"css selector","selector":"span[data-aura-class='uiImage']"} (会话信息:chrome=83.0.4103.61)

我的 WebDriver 是静态的,并且是从 Base 类派生的。我正在使用测试 7.1.0。可能是什么问题?

我在论坛上搜索,但没有找到答案。

【问题讨论】:

  • 您的 testng 配置是否设置了并行执行?
  • 没有。这是我的测试文件:ttps://testng.org/testng-1.0.dtd" rel="nofollow" target="_blank">testng.org/testng-1.0.dtd">

标签: selenium


【解决方案1】:

尝试使用 dependsOn 或在测试注释中使用优先级。 @Test 方法并不总是按照它们编写的顺序执行。此外,如果测试并行运行,这也是一个问题。 由于logOut完全依赖登录方式,尝试使用

@Test(dependsOnMethods ="login")
public void logOut() {
....}

dependsOn 将确保仅在登录方法成功完成或通过后运行 logOut 方法。

【讨论】:

  • 正确的注释是@Test(dependsOnMethods = "login"),它给出了预期的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-04
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多