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

问题描述:

我使用Selenium网络驱动程序自动化。
我正在尝试使用Gmail帐户验证撰写电子邮件。
我使用for循环对其进行编码,但它只是通过gmail登录,而不是现在编写验证方法后编写的。试图使用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("[email protected]"); 
    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); 
} 

代码在控制台中我得到这个消息时,我跑这

通过:verifyGmailLogin

失败:verifySendingEmail

但它应该通过两者都在运行..

以下是更多的信息是我的Xpath ..但是没有错误xpath

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

这是什么问题?或者你得到什么错误?或者你面临什么问题 –

+0

运行时没有错误,但是它停止并且浏览器在经过一段时间登录gmail帐户后关闭。 (由于调用driver.quite()方法)编写新邮件在这里不起作用。那么我在验证部分中是否有任何错误。 – thsumia

+0

我只需要知道这个验证码和'Assert.assertTrue(composePage.verifySentMessageSuccess2(20,“Selenium Web Driver Test”));'在verifySendingEmail方法中是正确的还是错误的 – thsumia

希望给出的xpath是错误的。由于您是硒的新手,请尝试下面的代码,它使用来检查整个页面driver.getPageSource()

因此,除了xpath,代码没有任何问题。

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

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; 
    } 
+0

@MrunalGosar '20'是显示在我的gmail收件箱页面下的邮件数量......是否将它作为参数传递是正确的?我在上面的问题中提到了xpath。 – thsumia