【问题标题】:how can skip a test in same class- selenium web driver如何跳过同一类中的测试 - selenium web 驱动程序
【发布时间】:2014-02-06 06:57:23
【问题描述】:

目前正在开发 Selenium Webdriver 并使用 Java.. 测试在 Firefox 26.0.. 在 Eclipse 中运行> 我正在使用 TestNG 框架..

  • 如果我正在运行测试名称 Test.java。因为我有许多过滤器部分组合与下拉值以及图像中显示的日期选择器和多选框。

基于过滤器部分,我编写了如下代码:

Log.info("Clicking on Visualization dropdown");

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('visualizationId').style.display='block';");   
Select select = new Select(driver.findElement(By.id("visualizationId")));
select.selectByVisibleText("Week");
Thread.sleep(6000);

Log.info("Clicking on Period dropdown");
JavascriptExecutor executor1 = (JavascriptExecutor)driver;
executor1.executeScript("document.getElementById('periodId').style.display='block';");
Select select1 = new Select(driver.findElement(By.id("periodId")));
select1.selectByVisibleText("Last 4 Weeks");
Thread.sleep(6000); 

Log.info("Clicking on Apply Filter button");
driver.findElement(By.id("kpiFilterSubmit")).click();// This is the one combination of filter section

//Filter selection-2

Log.info("Clicking on Visualization dropdown");
JavascriptExecutor executor3 = (JavascriptExecutor)driver;
executor3.executeScript("document.getElementById('visualizationId').style.display='block';");
Select select3 = new Select(driver.findElement(By.id("visualizationId")));
select3.selectByVisibleText("ICC");
Thread.sleep(6000);

 Log.info("Clicking on Type dropdown");
JavascriptExecutor executor02 = (JavascriptExecutor)driver;
executor02.executeScript("document.getElementById('classificationId').style.display='block';");
Select select02 = new Select(driver.findElement(By.id("classificationId")));
select02.selectByVisibleText("Internal PRs");
Thread.sleep(6000);

Log.info("Clicking on Priority dropdown");
JavascriptExecutor executor5 = (JavascriptExecutor)driver;
    executor5.executeScript("document.getElementById('priorityId').style.display='block';");
Select select5 = new Select(driver.findElement(By.id("priorityId")));
select5.deselectAll();
select5.selectByVisibleText("Not Urgent");
Thread.sleep(6000);

Log.info("Clicking on Apply Filter button");
driver.findElement(By.id("kpiFilterSubmit")).click();
Thread.sleep(6000);// Second combination of filter section.

例如,如果有时我无法识别元素或其他一些问题,则通常代码将停止并显示错误。但在我的情况下,我想跳过那个特定的过滤器部分,我需要移动到过滤器部分的其他组合。请帮助我以更好的标准编写代码。我正在学习 java 和 selenium 网络驱动程序。

【问题讨论】:

  • Jugaad 将对每个部分使用 try catch。理想情况下,您应该处理可能引发错误的每种情况,即使用适当的等待,检查元素是否存在,然后仅继续对其进行任何操作等。另一方面,您可以使用像 testNg 这样的框架来处理每个测试用例。
  • 可能有更好的方法:当您单击“应用过滤器”时,页面可能会根据下拉菜单中的值向服务器发送 URL 请求.因此,您不必选择所有这些值并单击“应用过滤器”,而是可以简单地构建 URL 字符串并使用 Web 驱动程序导航到它。如果您提供您尝试自动化的网页的 URL,那么我也许可以帮助您。
  • 我也有同样的问题,但仍在等待更好的解决方案...如果您找到了答案,请告诉我
  • 您能否提供这部分页面的html代码,我有一些想法可以改进您的代码,但需要更多细节

标签: java javascript selenium selenium-webdriver


【解决方案1】:

在我的情况下,我想跳过那个特定的过滤器部分,我需要 移动到过滤器部分的其他组合 块引用

您可以使用“验证”辅助方法。

维基:http://docs.seleniumhq.org/docs/02_selenium_ide.jsp#assertion-or-verification

例如,

JavascriptExecutor executor = (JavascriptExecutor)driver;
if(verifyElementPresent(By.id("visualizationId")){
    executor.executeScript("document.getElementById('visualizationId').style.display='block';")   
    Select select = new Select(driver.findElement(By.id("visualizationId")));
    select.selectByVisibleText("Week");
    Thread.sleep(6000);
}
...
        public boolean verifyElementPresent(By by) {
            try {
              driver.findElement(by);
              return true;
            } catch (NoSuchElementException e) {
              return false;
            }
          }

顺便说一句, 使用Thread.sleep(6000);不是一个好的决定

你可能会面临不同的问题。

所以尝试使用等待。 维基:http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp

【讨论】:

    【解决方案2】:

    您的目标不明确 - 应该只使用一个成功的组合,还是希望使用所有成功的组合运行测试?我可能会建议使用数据提供者。它为您的测试提供输入数据。在您的情况下,它可能是要使用的过滤器列表。该列表将在依赖@Test 或@BeforeTest 中进行管理。所有失败的组合都将被标记为失败并像往常一样跳过测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2022-10-15
      • 2012-09-02
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      相关资源
      最近更新 更多