我创建了两个页面类,以在一个程序包中找到两个网页的元素(名为 LoginPage.java 和 AddEmployee.java )。之后,在另一个包中,我分别为上述两个对应的类创建了两个测试类(分别名为 VerifyloginpageTest.java 和 VerifyAddEmployeeTest.java ),并且还创建了另一个名为< strong> BrowserFactory.java 来初始化浏览器并创建驱动程序实例。使用页面工厂,我要首先按优先级顺序运行登录页面测试用例,然后在成功登录网站后,应继续到添加员工网页。
**
**
public void SendToneAnalysis(string conversationString)
{
Runnable.Run( SendAssistantToneAnalysis(conversationString) );
}
private IEnumerator SendAssistantToneAnalysis(string conversationString)
{
if ( !_service.GetToneAnalyze(OnGetToneAnalyze, OnFail, conversationString) )
{
Log.Debug("ExampleToneAnalyzer.SendAssistantToneAnalysis()", "Failed to analyze!");
}
while (!_UserUtteranceToneTested)
yield return null;
}
**
package OrangeTestCases.Helper;
public class BrowserFactory {
public static WebDriver driver;
@BeforeClass
public static WebDriver startBrowser(String browsername,String url)
{
System.setProperty("webdriver.chrome.driver","C:\\Users\\int120\\Downloads\\EXE FILES\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
return driver;
}
**
package OrangeTestCases.orangeHRM.Pages;
public class LoginPage{
// WebDriver driver;
@FindBy(id="txtUsername")
WebElement username;
@FindBy(how=How.ID,using="txtPassword")
WebElement password;
@FindBy(how=How.ID,using="btnLogin")
WebElement loginBtn;
@FindBy(how=How.XPATH,using="//*[@id=\"spanMessage\"]")
WebElement errormsg;
@FindBy(how=How.XPATH,using="//*[@id=\"menu_dashboard_index\"]/b")
WebElement dashboard;
//public LoginPage(WebDriver driver) {
//
// this.driver=driver;
// }
public void typeUsername(String u_name)
{
username.clear();
username.sendKeys(u_name);
}
public void typePassword(String pass)
{
password.clear();
password.sendKeys(pass);
}
public void clickLogin()
{
loginBtn.click();
}
public String findErrorMsg()
{
String actual_error=errormsg.getText();
return actual_error;
}
public boolean findDashBorad()
{
boolean dashboard_display=dashboard.isDisplayed();
return dashboard_display;
}
}
**
package OrangeTestCases.orangeHRM.Pages;
public class AddEmployee {
@FindBy(how=How.XPATH,using="//*[@id=\"menu_pim_viewPimModule\"]/b")
WebElement pim;
@FindBy(how=How.XPATH,using="//*[@id=\"menu_pim_addEmployee\"]")
WebElement addemployee;
@FindBy(how=How.XPATH,using="//*[@id=\"firstName\"]")
WebElement f_name;
@FindBy(how=How.XPATH,using="//*[@id=\"middleName\"]")
WebElement m_name;
@FindBy(how=How.XPATH,using="//*[@id=\"lastName\"]")
WebElement l_name;
@FindBy(how=How.XPATH,using="//*[@id=\"photofile\"]")
WebElement photo;
@FindBy(how=How.XPATH,using="//*[@id=\"chkLogin\"]")
WebElement create_chkbox;
@FindBy(how=How.ID,using="user_name")
WebElement username;
@FindBy(how=How.XPATH,using="//*[@id=\"user_password\"]")
WebElement pswrd1;
@FindBy(how=How.ID,using="re_password")
WebElement confirm_emp_pswrd;
@FindBy(how=How.XPATH,using="//*[@id=\"status\"]")
WebElement status;
@FindBy(how=How.XPATH,using="//*[@id=\"btnSave\"]")
WebElement save;
// WebDriver driver;
//
//
// public AddEmployee(WebDriver driver)
// {
// this.driver=driver;
// }
public void clickPIM()
{
pim.click();
}
public void clickAddEmployee()
{
addemployee.click();
}
public void typefirstname(String fname)
{
f_name.sendKeys(fname);
}
public void typemiddlename(String mname)
{
m_name.sendKeys(mname);
}
public void typelastname(String lname)
{
l_name.sendKeys(lname);
}
public void uploadPhoto(String photo1)
{
photo.sendKeys(photo1);
}
public void clickCheckbox()
{
create_chkbox.click();
}
public void typeUsername(String u_name)
{
username.sendKeys(u_name);
}
public void typePassword(String pass)
{
pswrd1.sendKeys(pass);
}
public void confirmPassword(String con_pass)
{
confirm_emp_pswrd.sendKeys(con_pass);
}
public void selectStatus(String status_test)
{
Select status1=new Select(status);
status1.selectByVisibleText(status_test);
}
public void clickSavebtn()
{
save.click();
}
}
**
**
package OrangeTestCases.orangeHRM.Testcases;
import OrangeTestCases.Helper.BrowserFactory;
import OrangeTestCases.orangeHRM.Pages.LoginPage;
public class VerifyloginpageTest{
String url="https://opensource-demo.orangehrmlive.com/";
WebDriver driver=BrowserFactory.startBrowser("chrome", url);
LoginPage login=PageFactory.initElements(driver, LoginPage.class);
VerifyAddEmployeeTest verify_addEmployee=new VerifyAddEmployeeTest();
//LoginPageObjects loginPage = PageFactory.initElements(driver, LoginPageObjects.class);
//To verify that error message occur when username is wrong
@Test(priority=1)
public void verify2() throws InterruptedException
{
//LoginPage login=new LoginPage(driver);
Thread.sleep(2000);
login.typeUsername("adm");
login.typePassword("admin123");
login.clickLogin();
String actualError=login.findErrorMsg();
String expectedError="Invalid credentials";
Assert.assertEquals(actualError,expectedError);
}
//To verify that error message occur when password is wrong
@Test(priority=2)
public void verify3() throws InterruptedException
{
// LoginPage login=new LoginPage(driver);
Thread.sleep(2000);
login.typeUsername("admin");
login.typePassword("adm");
login.clickLogin();
String actualError=login.findErrorMsg();
String expectedError="Invalid credentials";
Assert.assertEquals(actualError,expectedError);
}
//To verify that error message occur both username and password are wrong
@Test(priority=3)
public void verify4() throws InterruptedException
{
// LoginPage login=new LoginPage(driver);
Thread.sleep(2000);
login.typeUsername("adm");
login.typePassword("adm");
login.clickLogin();
String actualError=login.findErrorMsg();
String expectedError="Invalid credentials";
Assert.assertEquals(actualError,expectedError);
}
//To verify that error message occur both username and password are empty
@Test(priority=4)
public void verify5() throws InterruptedException
{
// LoginPage login=new LoginPage(driver);
Thread.sleep(2000);
login.typeUsername("");
login.typePassword("");
login.clickLogin();
String actualError=login.findErrorMsg();
String expectedError="Username cannot be empty";
Assert.assertEquals(actualError,expectedError);
}
//To verify that error message occur password is empty
@Test(priority=5)
public void verify6() throws InterruptedException
{
// LoginPage login=new LoginPage(driver);
Thread.sleep(2000);
login.typeUsername("admin");
login.typePassword("");
login.clickLogin();
String actualError=login.findErrorMsg();
String expectedError="Password cannot be empty";
Assert.assertEquals(actualError,expectedError);
}
//To verify login is successful
@Test(priority=6)
public void verifyloginpage() throws Exception {
//login=new LoginPage( driver);
Thread.sleep(5000);
login.typeUsername("Admin");
login.typePassword("admin123");
login.clickLogin();
boolean actual_adminText=login.findDashBorad();
Assert.assertTrue(actual_adminText);
}
@Test(priority=7)
public void add_Employee()
{
verify_addEmployee.webPage(driver);
}
}
因此,当我运行VerifyloginpageTest.java类时,它成功执行了所有6个登录测试用例并登录了网站,但未执行AddEmployee页面。
答案 0 :(得分:0)
根据TestNG文档,如果testng.xml
文件不存在,则应创建该文件,然后根据需要对其进行配置。
示例testng.xml
文件:
<suite name="TestExample">
<test name="Login">
<classes>
<class name="packageOfYourTest.VerifyloginpageTest" />
</classes>
</test>
<test name="AddEmployee">
<classes>
<class name="packageOfYourTests.AddEmployee" />
</classes>
</test>
</suite>
输出将如下所示:
testBeforeSuite()
testBeforeTest()
testLogin()
testAfterTest()
testBeforeTest()
testAddEmployee()
testAfterTest()
答案 1 :(得分:0)
要使测试类使用TestNG并行运行,应创建testNG配置xml文件。
<suite name="SuiteName">
<test name="TestName">
<classes>
<class name="path.to.your.test.class.goes.here" />
</classes>
</test>
</suite>
请注意,实际上,使用Before方法可以创建线程,并且为每个线程创建驱动程序的新实例是一种很好的做法。
将并行属性添加到“ suite”标签上的配置文件中:
<suite name="SuiteName" parallel='classes'>
<test name="TestName">
<classes>
<class name="path.to.your.test.class.goes.here" />
</classes>
</test>
</suite>
您可以并行处理“方法”,“测试”或“实例”。但是由于您需要并行运行类,因此请使用适当的值。
现在,您还可以在“ suite”标签上添加属性“ thread-count”,以设置将并行运行的最大类数。如果您未指定此属性的值,则默认情况下,金额将等于5。
现在非常重要:每当调用@Be之前,TestNG都会创建新线程,并且需要为每个线程创建驱动程序并在确切的线程中使用它。为此,您必须将每个新的驱动程序实例放入ThreadLocal <>容器。
private static final ThreadLocal<WebDriver> DRIVER = new ThreadLocal<>();
public static WebDriver getDriver() {
return DRIVER.get();
}
@BeforeMethod
public void setUp() {
if (DRIVER.get() == null) {
DRIVER.set(new FirefoxDriver());
}
}
@AfterMethod
public void tearDown() {
if (DRIVER.get() != null) {
DRIVER.remove();
}
}
因此,您将使驱动程序成为线程安全的。另请注意,如果您在@BeforeClass中启动驱动程序,则需要在@AfterClass中启动driver.qiut()。如果从@BeforeSuite开始-@AfterSuite中的driver.qiut()等。为了确保一切正常,您可以在创建或终止驱动程序时将其打印到日志线程ID中。
更多信息在这里: http://testng.org/doc/documentation-main.html#parallel-running https://www.swtestacademy.com/selenium-parallel-tests-grid-testng/