【发布时间】: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