【发布时间】:2016-07-20 22:13:20
【问题描述】:
Selenium Webdriver:在此代码中,我的测试用例通过了一个测试电子邮件帐户,但对于第二个测试邮件它跳过了代码,它没有通过第二个测试电子邮件
package Packet;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class NewTest {
public WebDriver driver;
// = new ChromeDriver();
// System.setProperty("webdriver.chrome.driver","/Users/rohit/Desktop/chromedriver");
public String baseurl = "http://google.com";
@BeforeTest
public void launchbrowser()
{
System.out.println("LaunchBrowser called...");
System.setProperty("webdriver.chrome.driver", "/Users/rohit/Desktop/chromedriver");
driver = new ChromeDriver();
driver.get(baseurl);
}
// *[@id="gmail-sign-in"]//*[@id="gbw"]/div/div/div[1]/div[1]/a
@BeforeMethod
public void Beforemethod() {
System.out.println("Hello Before Method");
}
@Test(priority = 0)
public void GmailTap() {
System.out.println("Test priority 0 called...");
driver.findElement(By.xpath("//*[@id=\"gbw\"]/div/div/div[1]/div[1]/a")).click();
// driver.findElement(By.xpath("//*[@id=\"gmail-sign-in\"]")).click();
System.out.println("hello");
driver.findElement(By.id("Email")).sendKeys("abc@gmail.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.HOURS);
// Thread.sleep(8000);
// driver.close();
// findElement(By.cssSelector(""))
driver.findElement(By.name("signIn")).click();
// CharSequence charSequence = new
boolean flag = true;
while (flag) {
try {
driver.findElement(By.name("Passwd")).sendKeys("123456");
System.out.println("Executed");
flag = false;
} catch (Exception e) {
}
}
driver.findElement(By.id("signIn")).click();
driver.findElement(By.xpath("//*[@id=\"gb\"]/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
driver.findElement(By.xpath("//*[@id=\"gb_71\"]")).click();
}
// _____________
@Test(priority = 1)
public void GmailTap2() {
System.out.println("Test priority 1 called...");
// driver.findElement(By.xpath("//*[@id=\"gbw\"]/div/div/div[1]/div[1]/a")).click();
System.out.println("hello");
driver.findElement(By.id("Email")).sendKeys("abc1@gmail.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.HOURS);
driver.findElement(By.name("signIn")).click();
boolean flag = true;
while (flag) {
try {
driver.findElement(By.name("Passwd")).sendKeys("123456");
System.out.println("Executed");
flag = false;
} catch (Exception e) {
}
}
driver.findElement(By.id("signIn")).click();
driver.findElement(By.xpath("//*[@id=\"gb\"]/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
driver.findElement(By.xpath("//*[@id=\"gb_71\"]")).click();
}
// ____________
@AfterMethod
public void aftersignout1() {
driver.findElement(By.xpath("//*[@id=\"account-chooser-link\"]")).click();
driver.findElement(By.xpath("//*[@id=\"account-chooser-add-account\"]")).click();
}
@AfterTest
public void aftersignout() {
System.out.println("aftersignOut called...");
// New Accnt
driver.close();
}
// *[@id="account-chooser-add-account"]
}
输出显示为:
LaunchBrowser called...
Starting ChromeDriver 2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b) on port 25912
Only local connections are allowed.
Hello Before Method
Test priority 0 called...
hello
Executed
Hello Before Method
Test priority 1 called...
[340.657][SEVERE]: Unable to receive message from renderer
【问题讨论】:
-
为什么你一次又一次地为每个测试用例启动和销毁你的webdriver实例......尝试在@BeforeTest注解中启动一次并在@AfterTest注解处销毁它
-
@Noor ,我通过你的解释更新了我的代码,但它给了我一个错误,无法接收来自渲染器的消息。你能看看我的代码