尝试验证使用Gmail帐户撰写电子邮件

时间:2016-11-09 06:35:11

标签: selenium selenium-webdriver

我使用Selenium网络驱动程序自动化 我正在尝试使用Gmail帐户验证撰写电子邮件 我使用for循环对其进行编码,但它只是通过了gmail登录,但在编写验证方法后,即使现在也没有编写。

public boolean verifySentMessageSuccess2(int i, String expectedMessage) throws InterruptedException {
    boolean flag = false;

    String xpath="//tbody/tr/td[6]/div/div/div/span/b";
    List<WebElement> mailSubject = driver.findElements(By.xpath(xpath));


    for(int second = 0; second<=i; second++){
        if (second>=i){
            break;
        }
         for (WebElement elements : mailSubject) {
             if (mailSubject.contains(expectedMessage)) {
                 System.out.println("Mail is sent successfully. Mail is: " + mailSubject);
             flag = true;
             }
             }
             if (flag) {
                 System.out.println("Mail is not sent successfully.");
             break;
             }
             Thread.sleep(1000);
             }
    return flag;
}

上面是在GmailComposePage类中编码的。以下是与GmailComposeTest类相关的代码

public void verifySendingEmail() throws Exception{
    GmailComposePage composePage = new GmailComposePage(driver);
    composePage.clickCompose();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    composePage.enterRecipient("tsumia@gmail.com");
    composePage.enterSubject("Selenium Web Driver Test");
    composePage.enterMessage("This is a sample web driver email");
    composePage.clickSend();
    Thread.sleep(3000);
    composePage.navigateToInbox();
    Thread.sleep(3000);
    Assert.assertTrue(composePage.verifySentMessageSuccess2(20, "Selenium Web Driver Test"));
    Thread.sleep(5000);
}

在控制台中,当我运行此

时,我收到此消息

PASSED:verifyGmailLogin

失败:verifySendingEmail

但它应该在运行时传递两者..

有关下面的详细信息,请参阅我的Xpath ..但xpath中没有错误

String row_xpath2 = "//tbody/tr/td[6]/div/div/div/span/b";

1 个答案:

答案 0 :(得分:0)

希望给出的xpath是错误的。由于您不熟悉selenium,请尝试下面的代码,使用 driver.getPageSource()

检查整个页面

因此除了xpath之外,你的代码没有任何问题。

尝试使用Google搜索与xpath相关的主题。

public static boolean verifySentMessageSuccess2(int i, String expectedMessage) throws InterruptedException 
    {
        boolean flag = false;
        for(int second = 0; second<=i; second++)
        {
            if (second>=i)
            {
                break;
            }
            if (driver.getPageSource().contains(expectedMessage)) 
            {
                System.out.println("Mail is sent successfully. Mail is: " + expectedMessage);
                flag = true;
            }
            if (flag) 
            {
                System.out.println("Mail is sent successfully.");
                break;
            }
            Thread.sleep(1000);
        }
        return flag;
    }