使用TestNG的第一个测试用例
要遵循的步骤:
1)按Ctrl + N,在TestNG类别下选择“ TestNG Class ” ,然后单击Next。
要么
右键单击Test Case文件夹,转到TestNG并选择“ TestNG Class ”。
2)如果您的项目已设置并且您在创建TestNG类之前选择了“测试用例”文件夹,则源文件夹和包名称将在表单上预先填充。将类名设置为“ TestNG ”。
在Annotations下,选中“ @BeforeMethod ”,“ @ AfterMethod ”并单击Finish。而已。
3)现在它将在Test Case包(文件夹)下显示新创建的TestNg类。TestNG类看起来像下面的图像,显示三个空方法。一个方法f()默认情况下和方法之前和之后,在创建类时选择。
4)Project Explorer将使用TestNG类看起来像这样。
现在是时候编写第一个TestNG测试用例了。
5)让我们以第一个测试用例为例,将测试用例分为三个部分。
@BeforeMethod:启动Firefox并将其指向基本URL
@Test:输入登录用户名和密码,打印控制台消息并注销
@AfterMethod:关闭Firefox浏览器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | package automationFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
public class TestNG {
public WebDriver driver;
@Test
public void main() {
// Find the element that's ID attribute is 'account'(My Account)
driver.findElement(By.id("account")).click();
// Find the element that's ID attribute is 'log' (Username)
// Enter Username on the element found by above desc.
driver.findElement(By.id("log")).sendKeys("testuser_1");
// Find the element that's ID attribute is 'pwd' (Password)
// Enter Password on the element found by the above desc.
driver.findElement(By.id("pwd")).sendKeys("Test@123");
// Now submit the form. WebDriver will find the form for us from the element
driver.findElement(By.id("login")).click();
// Print a Log In message to the screen
System.out.println(" Login Successfully, now it is the time to Log Off buddy.");
// Find the element that's ID attribute is 'account_logout' (Log Out)
driver.findElement(By.id("account_logout"));
}
@BeforeMethod
public void beforeMethod() {
// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
//Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch the Online Store Website
driver.get("http://www.onlinestore.toolsqa.wpengine.com");
}
@AfterMethod
public void afterMethod() {
// Close the driver
driver.quit();
}
} |
6)右键单击测试用例脚本运行测试,然后选择Run As > TestNG Test。
运行Testng测试用例的结果
7)完成执行几分钟后,一旦完成,结果将在TestNg Result窗口中显示如下。
它显示'传递:1'。这意味着测试成功并通过。
有3个子标签。“所有测试”,“测试失败”和“摘要”。只需单击“所有测试”即可查看其中的内容。
如您所见,有关于执行哪些测试用例及其持续时间的信息。看看其他标签。比Junit好吗?
8)TestNG还生成HTML报告。要访问这些报告,请转到Project目录并打开test-output文件夹。
9)打开' emailable-report.html ',因为这是一个用浏览器打开它的html报告。
10)TestNG还生成' index.html '报告,它位于同一个测试输出文件夹中。此报告提供指向TestNG报告的所有不同组件(如组和报告输出)的链接。单击这些将显示执行的详细描述。在TestNG的高级章节中,我们将介绍每个TestNG主题。