0
public class TestingLogin {

    WebDriver driver;
    @BeforeTest
    public void setup()
    {
        System.setProperty("webdriver.gecko.driver", "\\GeckoDriver\\geckodriver.exe");
        driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://test.admin.placer.life/login");
    }

    @Test(priority=9)
    public void reset_null_email()
    {
        LoginPage login=new LoginPage(driver);
        driver.findElement(By.xpath("//*[@id='app']/div/div[2]/a")).click();
        WebDriverWait wait = new WebDriverWait(driver, 60);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/p[contains(text(),'Reset Password')]")));
        login.set_reset_email("");
        login.click_reset_button(); 
        WebDriverWait wait_reset_psw = new WebDriverWait(driver, 10);
        wait_reset_psw.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")));
        Assert.assertEquals(driver.findElement(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")).getText(),"The email field is required.");
        driver.navigate().refresh();
    }

    @Test(priority=10)
    public void reset_invalid_email()
    {
        LoginPage login=new LoginPage(driver);
        driver.findElement(By.xpath("//*[@id='app']/div/div[2]/a")).click();
        WebDriverWait wait = new WebDriverWait(driver, 120);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='app']/div/div[2]/p[contains(text(),'Reset Password')]")));
        login.set_reset_email("a@gmail.com");
        login.click_reset_button(); 
        WebDriverWait wait_reset_psw = new WebDriverWait(driver, 10);
        wait_reset_psw.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")));
        Assert.assertEquals(driver.findElement(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")).getText(),"The email field is required.");
        driver.navigate().refresh();
    }       
    @AfterTest
    public void close()
    {
        driver.close();
    }

}

大家好,我已经尝试过使用“ visibilityOfElementLocated ”的reset_invalid_email() ,但由于代码块的相似性,我猜这是导致测试用例失败的原因。所以我用“ elementToBeClickable ”进行了尝试。但它也导致了同样的结果错误。

请帮忙

提前致谢

4

1 回答 1

0

根据您提到的reset_invalid_email()方法问题,您可以使用以下解决方案:

几句话:

  • 每当您打算单击元素使用elementToBeClickable()方法时。
  • Firefox 浏览器以最大化模式打开,因此您无需使用driver.manage().window().maximize();
  • 如果您需要最大化任何浏览器,请改用*Options类。
  • 始终driver.quit()tearDown(){}方法内调用以优雅地关闭和销毁WebDriverWeb 客户端实例。
  • 您的工作代码块:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.Assert;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    
    public class q50598754 {
    
        WebDriver driver;
        @BeforeTest
        public void setup()
        {
            System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
            driver=new FirefoxDriver();
            driver.get("https://test.admin.placer.life/login");
        }
    
        @Test(priority=10)
        public void reset_invalid_email()
        {
            //LoginPage login=new LoginPage(driver);
            new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("I forgot my password"))).click();
            new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/p[contains(text(),'Reset Password')]")));
            //login.set_reset_email("a@gmail.com");
            //skipped to reproduce the error message The email field is required
            //login.click_reset_button(); 
            driver.findElement(By.cssSelector("button.btn.btn-primary.btn-block.btn-flat")).click();;
            Assert.assertEquals(driver.findElement(By.cssSelector("div.form-group.has-feedback.has-error span.help-block")).getText(),"The email field is required.");
        } 
    
        @AfterTest
        public void close()
        {
            driver.quit();
        }
    }
    
  • 控制台输出:

    PASSED: reset_invalid_email
    
    ===============================================
        Default test
        Tests run: 1, Failures: 0, Skips: 0
    ===============================================
    
    
    ===============================================
    Default suite
    Total tests run: 1, Failures: 0, Skips: 0
    ===============================================
    
于 2018-05-30T08:57:45.367 回答