java - Selenium TestNG 依赖澄清设计
问题描述
当我使用 UDEMY 课程试验 TestNG 时,我需要澄清依赖部分。下面我有一个方法可以登录到 gmail,称为 gmailLogin()。我有一个单独的方法可以在 Gmail 搜索框中搜索主题(一旦您登录),称为 gmailSearch()。
您需要登录到您的 Gmail 帐户才能执行搜索。我做了两件事来做实验
1) 在 gmailLogin() 中提供了不正确的信息。这将失败。2) 我没有在 gmailSearch() 中使用 dependsOnMethods="gmailLogin"。
测试 gmailSearch() 不会失败,因为它使用来自 @BeforeMethod 的 google 主页搜索。Google 主页的搜索也使用 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的更多信息。
推荐阅读
- assembly - MIPS CPU 如何掉电?
- python - Errno 98 自动脚本在重新启动时启动时地址已在使用中
- python - 在 Jupyter Notebook 上导入 Keras
- node.js - Docker Compose 导致 Error: listen EADDRINUSE: address already in use :::3003
- ansible - 编写 ansible 剧本的方法
- mat-table - 如何修复 angulat-mat-table 中的行高
- symfony - 在 API 平台安全注解中使用常量
- eclipse - 在 Cloudera HDP 沙盒上安装 Eclipse
- c++ - C++ Visual Studio C2027 和 C2011 错误
- python - 条件排序,在 pandas 的列上使用 groupby