测试通过但未发生动作 selenium webdriver + testng [英] Test passed but action did not happen selenium webdriver + testng

查看:28
本文介绍了测试通过但未发生动作 selenium webdriver + testng的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 selenium webdriver 的新手,我在 IE 8 上使用 2.31 版本,testng 6.8 和 fire 测试.我在这个方案中编写我的测试:我有测试类,其中有带有 testng @Test 注释的方法.它看起来像这样:

I'm quite new in selenium webdriver, I'm using 2.31 version, testng 6.8 and fire tests on IE 8. I'm writting my tests in this scheme: I have test classes where i have methods with testng @Test annotation. It looks like this:

@Test(description="click Save Button ", dependsOnMethods = { "edit form" })
public void clickSaveButton(ITestContext context) {
    page.clickSaveButton(driver);

}

然后,如您所见,我有页面类,我在其中存储元素 ID、xpath 等.它看起来像这样:

Then, as you can see I have page class where I store elements ids, xpaths etc. It lokks like this:

public void clickSaveButton(WebDriver driver){
    Configuration.clickfoundElement(By.id(conf.get("saveButton")), driver);
}

conf 是代表属性文件的对象.最后我有配置类,我在那里做一些这样的思考:

conf is object that represents properties file. At last I have Configuration class where I do somethink like this:

public static void clickfoundElement(By by, WebDriver driver){
    int attempts = 0;

    while(attempts < 10) {
        try {

            driver.findElement(by).click();
            break;
        } catch(NoSuchElementException e) {
            System.out.println("NoSuchElementException");
            Reporter.log("NoSuchElementException<br/>");
            if(attempts==9){
                throw(e);

            }
        }
        catch(StaleElementReferenceException e) {
            System.out.println("StaleElementReferenceException");
            Reporter.log("StaleElementReferenceException<br/>");
            if(attempts==9){
                throw(e);
            }
        }}

这可以防止我遇到 NoSuchElementException 和 StaleElementReferenceException 并且工作得很好.

That prevents me from having NoSuchElementException and StaleElementReferenceException and works quite fine.

我的第一个问题是这种方法是否正确?第二个也是最重要的问题是,我不时遇到以下问题:

My first question is if this approach is correct? Second and the most important question is that from time to time I have following problem:

Testng 说clickSaveButton"(在最终报告中)通过了,但实际上并没有发生 clickSaveButton 动作(我可以在测试过程中看到我的浏览器).在下一个测试的最后,我有NoSuchElementException"(特别是当下一个测试不是关于点击某些东西而只是关于从 html 组件获取文本时).当然,这个 NoSuchElementException 发生是因为我真的没有要寻找的元素(因为上次测试操作没有发生,所以我仍然在上一个站点,没有这个元素)你能告诉我为什么会发生这种情况吗(重要的并不总是但只是有时)以及如何预防?

Testng says that"clickSaveButton" (in final report) is passed, but in fact clickSaveButton action did not happen (I can see it watching my browser during test). At the end in next test I have "NoSuchElementException" (especially when next test is not about clicking in something but only about getting text from html component). Of course this NoSuchElementException happens because there is really no element I am looking for (because last test action did not happen so I am still at the previous site, without this element) Can you tell me why this situation happen (what is important not always but only sometimes) and how to prevent it?

提前致谢.

推荐答案

感谢大家的参与.事实证明,我的问题发生是因为我在测试的应用程序中有类似弹出窗口的东西(确切地说是richFaces rich:modalPanel).这导致此弹出窗口后面"的 DOM 仍然可见,但因此无法单击其中的元素.解决我的问题只是等待元素通过 ExpectedConditions.elementToBeClickable(By by) 可点击的时间稍长一点,这确保我的弹出窗口在关闭它并点击另一个元素后消失.这使得 Ardesco 的回答是最好的.再次感谢您.

Thank you all for your time. It turned out that my problem occured because I had something like popup winowow in application I was testing (richFaces rich:modalPanel to be exact). That cause DOM "behind" this popup was still visible, but elements in it could no be clicked because of it. What solved my problem was just waiting a little bit longer for element to be clickable via ExpectedConditions.elementToBeClickable(By by) which made sure my popup disapeared after closing it and clicking another element is possible. That makes Ardesco answer the best. Thank you one more time.

这篇关于测试通过但未发生动作 selenium webdriver + testng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆