我已经尝试过以下代码来自动化gmail,在第一个函数中,浏览器能够获取元素直到“Compose”并点击它,但是找不到像“to”,“subject”这样的下几个元素。 .i已经指定了void mailSend()函数中的下一个元素,我不确定它为什么不读取下一个元素。
public class Example{
public static WebDriver driver;
@BeforeClass
public void before()
{
driver = new FirefoxDriver();
}
@Test(dataProvider = "Data-Provider-Function")
public void startup(String uName,String pass) throws Exception
{
driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys(uName);
driver.findElement(By.id("Passwd")).sendKeys(pass);
driver.findElement(By.id("signIn")).click();
Thread.sleep(4000);
driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji T-I-" +
"KE L3']")).click();
Thread.sleep(4000);
}
@DataProvider (name = "Data-Provider-Function")
public Object[][] startupProvider()
{
return new Object[][]
{
{"selva.prokarma.test@gmail.com", "prokarma"}
};
}
@Test(dataProvider="Mail Information")
public void mailSend(String to,String subject,String body) throws Exception
{
driver.navigate().refresh();
Thread.sleep(2000);
driver.findElement(By.className("vO")).sendKeys(to);
Thread.sleep(2000);
driver.findElement(By.className("aoT")).sendKeys(subject);
Thread.sleep(2000);
driver.findElement(By.cssSelector(".editable")).click();
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@tabindex,'1') and contains(@frameborder,'0')]")));
driver.findElement(By.xpath("//body[@role='textbox']")).sendKeys(body);
driver.switchTo().defaultContent();
driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji aoO T-I-atl L3']")).click();
Thread.sleep(4000);
driver.navigate().refresh();
Thread.sleep(4000);
}
@DataProvider (name = "Mail Information")
public Object[][] mailSendProvider()
{
return new Object[][]
{
{"selva.prokarma.test@gmail.com", "This is a Test Mail","Prokarma"},
{"selva.prokarma.test@gmail.com", "This is Test Mail 2","Hello Automation King "},
{"selva.prokarma.test@gmail.com","This is another Test Mail 3","Hello Selva"},
{"selva.prokarma.test@gmail.com","This is another Test Mail 3","Hello SelvaKumar"},
{"selva.prokarma.test@gmail.com","Hi How are you doing","Robotium Tasks to be followed"}
};
}
@AfterClass
public void tear()
{
driver.quit();
}
}
答案 0 :(得分:0)
我将等待时间保持为60秒,并删除了根本不需要的所有睡眠声明。我用单一的测试方法尝试了一切。
public class Example2 {
public static WebDriver driver;
@BeforeClass
public void before()
{
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60000, TimeUnit.MILLISECONDS);
}
@Test(dataProvider="Mail Information")
public void mailSend(String to,String subject,String body) throws Exception
{
driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("xyzy@gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("*********");
driver.findElement(By.id("signIn")).click();
driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji T-I-KE L3']")).click();
driver.findElement(By.className("vO")).sendKeys(to);
driver.findElement(By.className("aoT")).sendKeys(subject);
driver.findElement(By.cssSelector(".editable")).click();
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@tabindex,'1') and contains(@frameborder,'0')]")));
driver.findElement(By.xpath("//body[@role='textbox']")).sendKeys(body);
driver.switchTo().defaultContent();
driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji aoO T-I-atl L3']")).click();
driver.navigate().refresh();
}
@DataProvider (name = "Mail Information")
public Object[][] mailSendProvider()
{
return new Object[][]
{
{"xyzy@gmail.com", "This is a Test Mail","hello"},
};
}
@AfterClass
public void tear()
{
driver.quit();
}
附加输出截图...