当ATUreporter未在失败的测试中截取屏幕截图时,如何更改浏览器页面

时间:2015-02-25 08:41:44

标签: testing selenium webdriver testng

我故意测试失败只是为了看它是否需要截图,密码_field元素不能位于网页上,

因为我给错了xpath - passwordqqq(在下面的代码中查找注释)

当测试失败时,必须截屏,但事实并非如此,

所以我想使用setCaptureScreen方法更改浏览器页面,

我在这里查看了api,但记录不清。

以下代码执行此操作:

  • 断言显示2个字段(密码,电话)
  • not null
  • 清除每个字段
  • 输入手机
  • 输入密码
  • 如果密码/电话无效,
  • 断言它会停留在同一页面上

    package erjan.testNG.personal_cabinet_login;
    import java.io.IOException;
    
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.WebElement;
    
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    import org.testng.Assert;
    
    import org.testng.annotations.Listeners;
    
    import org.testng.annotations.Test;
    
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.AfterMethod;
    import atu.testng.reports.ATUReports;
    import atu.testng.reports.utils.Utils;
    import atu.testng.reports.listeners.ATUReportsListener;
    import atu.testng.reports.listeners.ConfigurationListener;
    import atu.testng.reports.listeners.MethodListener;
    import atu.testng.reports.logging.LogAs;
    import atu.testng.selenium.reports.CaptureScreen;
    import atu.testng.selenium.reports.CaptureScreen.ScreenshotOf;
    
    @Listeners({ ATUReportsListener.class, ConfigurationListener.class,
    MethodListener.class })
    
    public class Login_test_no_user {
    
        {
            System.setProperty("atu.properties", 
     "C:\\Users\\www\\ERJAN_NAIMIKZ_ALL\\erjan_2_naimikz_test\\ATUReporter_Selenium_testNG_5.5 BETA");
        }
    
          WebDriver firefox ;
          @Test
          public void main() {
          setAuthorInfoForReports();
          setIndexPageDescription();
    
          WebElement phone_field = null ;
          WebElement password_field = null ;
    
          //setCaptureScreenBrowserpage
    
          ATUReports.add("checking phone, password fields are displayed", "--", "--", "--", LogAs.INFO,null);
    
          try{
           phone_field= firefox.findElement(By.xpath("//*[@id=\"login-form\"]/div[1]/span/input"));
    
          Assert.assertNotNull(phone_field);
          Assert.assertNotNull(password_field);
    
          Assert.assertTrue(phone_field.isDisplayed());
          Assert.assertTrue(password_field.isDisplayed());
          }
          catch(Exception e){
              ATUReports.add("a field is invisible or not available", LogAs.FAILED, new CaptureScreen(
                      ScreenshotOf.BROWSER_PAGE)); 
          }
    
          ATUReports.add("phone, password are visible",LogAs.PASSED,null);
    
          ATUReports.add("inputting number", "0009999999", "--", "--", LogAs.INFO,null);
          phone_field.clear();
          phone_field.sendKeys("0009999999");
    
          ATUReports.add("inputting password", "bad_password!!!22222", "--", "--", LogAs.INFO,null);
          password_field.clear();
          password_field.sendKeys("bad_password!!!22222");
    
          ATUReports.add("click LOGIN button", "--", "--", "--", LogAs.INFO,null);
          firefox.findElement(By.cssSelector("input.button.blue")).click();
    
          String newUrl = firefox.getCurrentUrl();
    
          ATUReports.add("check URL", "astana/login", "astana/login", "--", LogAs.INFO,null);
          try{
              Assert.assertEquals("http://test.naimi.me/astana/login", newUrl);  
          }catch(Exception e ){
              ATUReports.add("webpages are not same!", LogAs.FAILED, new CaptureScreen(
                      ScreenshotOf.BROWSER_PAGE)); 
          }
    
          ATUReports.add("page is same",LogAs.PASSED,null);
    
      }
    
      private void setAuthorInfoForReports() {
           ATUReports.setAuthorInfo("NAIMI.KZ AUTOTEST", Utils.getCurrentTime(),"1.0");
        }
    
      private void setIndexPageDescription() {
          ATUReports.indexPageDescription = "erjan runs ATUreport";
    }
    
    
      @BeforeMethod
      public void beforeMethod() throws IOException {     
    
          firefox = new FirefoxDriver();
          ATUReports.setWebDriver(firefox);
    
    
          firefox.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
          firefox.get("http://test.naimi.me/astana/login"); 
    
      }
    
      @AfterMethod
      public void afterMethod() {
          firefox.quit();
      }
    
    }
    

1 个答案:

答案 0 :(得分:1)

请不要使用Exception。例如,有特定的处理程序:

@Test
public void sc01_2252015(){
    int x = 9, y = 10;

    try{
        Assert.assertEquals(x, y);
    }catch (AssertionError e) {
        System.err.println("NO!");
        throw new TestNGException("Error: " + e);
    } catch (Exception e) {
        System.err.println("NOPE!");
        throw new TestNGException("Error: " + e);
    }
}

NOPE!将无法执行