标签:pop fine should logic repo example browser ide info
Original URL: https://www.guru99.com/introduction-testng-groups.html
TestNG is a Testing framework that covers different types of test designs like unit, functional, end to end, UI and integration test.
You can run a single or multiple packages (package here means to encapsulate a group of classes in a proper director format) by creating XML and run it through maven.
In this tutorial, you will learn-
Set maven and Java path in environment variable (for windows user)
Another mechanism instead of Grouping is “exclude” or “include” in test XML
We use groups in Testng when,
In below example, we have shown the syntax of how to use groups in the XML file.
@Test (groups = { "bonding", "strong_ties" })
Here we are using 2 group names i.e. "bonding" and "strong_ties" (these are logical name that can be altered as per your wish).
<groups> tag defines the starting of groups in XML.
Customize your XML to pick the mentioned group from the test classes. Below mentioned is the syntax of how to declare groups in XML file e.g.
<groups> <run> <include name="bonding" /> </run> </groups>
So, let us assume that there are 10 test methods in a class.
Out of them,
Moving forward, we are going to set maven/Java path and use the Eclipse IDE to demonstrate the usage of groups using XML files in Java based maven project.
Please refer https://www.guru99.com/maven-jenkins-with-selenium-complete-tutorial.html
https://www.guru99.com/install-java.html
Multiple tags are used in a sequence to build a working testNG xml like <suite>, <test> and <class>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Guru 99 Smoke Test Demo"> <groups> <run> <include name="strong_ties" /> </run> </groups> <classes> <class name="com.group.guru99.TC_Class1" /> </classes> </test> </suite>
We will be using this XML for upcoming video downside.
Suppose you are finding the usage of group mechanism complex then testNG XML facilitate the functionality to exclude/include a test.
Note: We can include/exclude multiple test cases once at a time, and it works with Groups as well.
Explanation of the Java Code and XML with the group, exclude and include the tag in XML.
Note: Each step which you code should be declared in separate methods, but when executed, it will execute test methods depending upon the entries in the XML file.
Method 1: Initialize Browser and launch URL (tc01LaunchURL())
Method 2: Verify Login Page Heading (tc02VerifyLaunchPage())
Method 3: Enter userName and Password on login form (tc03EnterCredentials())
Method 4: Verify the presence of Manager ID on User Dashboard (tc04VerifyLoggedInPage())
Method 5: Verify few more links on User DashBoard (tc05VerifyHyperlinks())
Code for our scenario:
package com.group.guru99; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test; public class TC_Class1 { public static final WebDriver webDriver = new FirefoxDriver();; String launchPageHeading = "//h3[text()=‘Guru99 Bank‘]"; final String userName_element = "//input[@name=‘uid‘]", password_element = "//input[@name=‘password‘]", signIn_element = "//input[@name=‘btnLogin‘]"; final String userName_value = "mngr28642", password_value = "ydAnate"; final String managerID = "//td[contains(text(),‘Manger Id‘)]"; final String newCustomer = "//a[@href=‘addcustomerpage.php‘]", fundTransfer = "//a[@href=‘FundTransInput.php‘]"; /** * This test case will initialize the webDriver */ @Test(groups = { "bonding", "strong_ties" }) public void tc01LaunchURL() { webDriver.manage().window().maximize(); webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); webDriver.get("http://www.demo.guru99.com/V4/"); } /** * Will check the presence of Heading on Login Page */ @Test(groups = { "bonding" }) public void tc02VerifyLaunchPage() { Assert.assertTrue(webDriver.findElement(By.xpath(launchPageHeading)).isDisplayed(), "Home Page heading is not displayed"); System.out.println("Home Page heading is displayed"); } /** * This test case will enter User name, password and will then click on * signIn button */ @Test(groups = { "bonding", "strong_ties" }) public void tc03EnterCredentials() { webDriver.findElement(By.xpath(userName_element)).sendKeys(userName_value); webDriver.findElement(By.xpath(password_element)).sendKeys(password_value); webDriver.findElement(By.xpath(signIn_element)).click(); } /** * This test case will verify manger‘s ID presence on DashBoard */ @Test(groups = { "strong_ties" }) public void tc04VerifyLoggedInPage() { Assert.assertTrue(webDriver.findElement(By.xpath(managerID)).isDisplayed(), "Manager ID label is not displayed"); System.out.println("Manger Id label is displayed"); } /** * This test case will check the presence of presence of New customer link * And FundTransfer link in Left pannel */ @Test(groups = { "bonding" }) public void tc05VerifyHyperlinks() { Assert.assertTrue(webDriver.findElement(By.xpath(newCustomer)).isEnabled(), "New customer hyperlink is not displayed"); System.out.println("New customer hyperlink is displayed"); Assert.assertTrue(webDriver.findElement(By.xpath(fundTransfer)).isEnabled(), "Fund Transfer hyperlink is not displayed"); System.out.println("Fund Transfer hyperlink is displayed"); } }
Please Note: The credentials are only valid for 20 days, so if you are trying to run code on your local machine, so you might face invalid credentials error. Please find below steps to generate your login credentials:
Explanation of Code:
As mentioned above, we have created 5 test cases for performing each action in independent methods.
You can observe that to every method, we have associated a group parameter holding some value in it.
Basically, these are the name of the differentiating groups i.e. "strong_ties" & "bonding".
So overall, we have 4 scenarios;
4.Last, we are using include test mechanism to include the test cases (tc01LaunchURL, tc03EnterCredentials and tc05VerifyHyperlinks)
Please download code from the mentioned URL, it will contain all type of testXML:
Conclusion
We have learned here relatively a new way for running test cases using XML in Maven project.
We started by providing a brief introduction on testNG and continued with the full technical specification of Groups, exclude and include.
[Selenium+Java] Introduction to TestNG Groups
标签:pop fine should logic repo example browser ide info
原文地址:https://www.cnblogs.com/alicegu2009/p/9098765.html