由于我正在使用 UDEMY 课程试验 TestNG,我需要澄清依赖部分。 下面我有一个名为 gmailLogin() 的方法可以登录 gmail。我有一个单独的方法可以在Gmail搜索框中搜索主题(登录后),称为gmailSearch()。
您需要登录您的Gmail帐户来执行搜索。我做了两件事来做实验
1) 在 gmailLogin() 中提供了不正确的信息。这将失败。2)我没有在gmailSearch()中使用dependsOnMethods=“gmailLogin”。
测试gmail Search()不会失败,因为它使用来自@BeforeWay的谷歌主页搜索。谷歌主页的搜索也使用name='q'。
问:设计 gmailSearch() 方法以强制使用 gmailLogin() 的好方法是什么?如果当前流程设计不佳,那么我应该将登录和搜索结合在一种方法中吗?
提前感谢您花时间解释/回答。
public class GoogleTest {
static WebDriver driver;
@BeforeMethod
public void setUp(){
System.setProperty("webdriver.chrome.driver", "path");
driver=new ChromeDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
}
@Test(priority=1)
public void googleSearch(){
driver.findElement(By.xpath("//input[@name='q']")).sendKeys("news");
driver.findElement(By.xpath("//input[@value='Google Search']")).click();
if(driver.getPageSource().contains("www.foxnews.com")){
System.out.print("Search found");
}
}
@Test(priority=2,groups="Gmail")
public void gmailIcon(){
driver.findElement(By.xpath("//a[@href='https://mail.google.com/mail/?tab=wm']")).click();
if(driver.getTitle().contains("Gmail")){
System.out.print("Gmail found");
}
}
@Test(priority=2,groups="Gmail")
public void gmailLogin(){
WebDriverWait wait = new WebDriverWait(driver,30);
driver.get("https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin");
driver.findElement(By.xpath("//input[@type='email']")).sendKeys("aname@gmail.com");
driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@type='password']")));
driver.findElement(By.xpath("//input[@type='password']")).sendKeys("psd123");
driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click();
if(driver.getTitle().contains("Inbox")){
System.out.print("Gmail Inbox");
}
}
@Test(groups="Gmail")
public void gmailSearch(){
driver.findElement(By.xpath("//input[@name='q']")).sendKeys("QA"+ "\n");
if(driver.getTitle().contains("Search Results")){
System.out.print("Gmail Search");
}
}
@AfterMethod
public void testDown(){
driver.quit();
}
}
你只有一个类,这不是一个好主意,你需要为每个页面有单独的类。最好使用POM(页面对象模型)。在你的例子中,你有两个不同的页面,登录页面和Gmail页面。所以你需要为每个页面一个类。然后你可以为你的测试用例有类。例如,登录和搜索,在这个类中你可以调用登录和搜索。你还需要验证登录,然后开始搜索(你可以有一个测试来检查用户名,以确保用户登录,然后如果可以,你可以执行测试)。使用POM将帮助您更好地管理测试,尤其是如果您的测试项目很大。您可以在此处阅读有关POM的更多信息。
对于你的第一个问题,你应该使用依赖方法
来创建一个方法依赖项。也就是说方法B应该只在方法A之后执行,那么方法B应该有依赖方法="方法A"
@Test(取决于方法="方法A")
如果你是Selenium自动化的新手,你的测试看起来不错。但它可以更好地改进。您正在尝试在单个类中处理所有内容。
该类执行以下操作
理想情况下,每个类应该只有一个责任。我建议你看看下面的网站,以获得更好的设计