当selenium自动化testing用例场景失败时,如何报告我的Excel表格失败?

情景是:

1)打开浏览器。

2)导航到应用程序URL。

3)这里来了棘手的部分:

驱动程序几乎大部分时间都可以使用sendkeys()方法input用户名和密码。

但在极less数情况下,驱动程序无法执行操作,login页面只能冻结。

这是我的脚本:

driver.findElement(XPATH).sendKeys(userName); driver.findElement(XPATH).sendKeys(password); driver.findElement(XPATH).click(); // waiting for landing page to load Thread.sleep(70000); elib.setExcelData("DATA", 1, 4, "PASS"); 

当一切正常,它inputPASS在我的Excel表单。 现在,在login页面挂起的情况下,如何在excel表格中inputFAIL?

注意:我是使用Java语言的selenium webriver。

  driver.findElement(By.locator).sendkeys("username"); driver.findElement(By.locator).sendkeys("password"); driver.findElement(By.locator).click(); //Instead of thread.wait use explicit wait to wait for the next page to load (timeout set 30 sec) increase or decrease timeout as per your needs WebDriverWait wait = new WebDriverWait(driver, 30); try{ //any element on the homepage(page after login) wait.until(ExpectedConditions.visibilityOfElementLocated(locator)); }catch(Exception e) {//if this block executes then assume that your hang in the login page //you can also check if sendkeys has worked by getting the value from the username and password fields driver.findElement(By.locator).getText(); or getAttribute('value'); //then write fail into excel sheet } //continue with your test 

希望这可以帮助你…请回来,如果你需要任何进一步的帮助

这是为什么你需要一个testing执行框架的典型例子。 在这种情况下,当你的testing失败时,在代码写入结果到Excel电子表格之前,会出现某种exception。 你基本上需要使用一个工具(这里有太多的工具可以列出,但是JUnit,TestNG是一些基本的工具),它们会运行你的testing,然后你可以从这个工具中将合格/不合格的结果提取到你的电子表格中或者使用他们的报告)。