简体   繁体   English

在Selenium WebDriver中设置优先级

[英]Set Priority in Selenium WebDriver

I am working with selenium webdriver. 我正在使用selenium webdriver。 I have written 2 methods 我写过两种方法

1) addUserWithOutFirstName() 2) addUserWithOutGeneratingLicenceKey() 1)addUserWithOutFirstName()2)addUserWithOutGeneratingLicenceKey()

Initially the priority was set 0 for addUserWithOutFirstName() and 1 for addUserWithOutGeneratingLicenceKey(). 最初,addUserWithOutFirstName()的优先级设置为0,addUserWithOutGeneratingLicenceKey()的优先级设置为1。 Now i want to change the priority of addUserWithOutGeneratingLicenceKey() as 0 and addUserWithOutFirstName() as 1. I have changed the priorities. 现在我想将addUserWithOutGeneratingLicenceKey()的优先级更改为0,将addUserWithOutFirstName()更改为1.我已更改优先级。 But still addUserWithOutFirstName() is getting executed first. 但仍然首先执行addUserWithOutFirstName()。

Don't know what is the problem. 不知道是什么问题。 Can anybody help? 有人可以帮忙吗?

/* Add user without generating licence key */ / *添加用户而不生成许可证密钥* /

@Test(priority=0)
public void addUserWithOutGeneratingLicenceKey() throws BiffException, IOException,InterruptedException {

    try {
        int successfullLoginRowNumber = 6;
        Thread.sleep(2000);
        WebElement username = webElement("VAR_EMAIL");
        username.clear();
        username.sendKeys(getCellContent(0, successfullLoginRowNumber));
        Thread.sleep(2000);
        WebElement password = webElement("VAR_PASSWORD");
        password.clear();
        password.sendKeys(getCellContent(1, successfullLoginRowNumber));
        Thread.sleep(2000);
        WebElement signInButton = webElement("VAR_SIGNINBUTTON");
        signInButton.click();
        Thread.sleep(2000);
        input(properties.getProperty("VAR_ADDUSERDETAILS"));
        int emptyLicenceKeyRowNumber = 1;
        WebElement firstName = webElement("VAR_FIRSTNAME");
        firstName.clear();
        firstName.sendKeys(getCellContent(0, emptyLicenceKeyRowNumber));
        Thread.sleep(1000);
        WebElement lastName = webElement("VAR_LASTNAME");
        lastName.clear();
        lastName.sendKeys(getCellContent(1, emptyLicenceKeyRowNumber));
        Thread.sleep(1000);
        WebElement email = webElement("VAR_ADDUSEREMAIL");
        email.clear();
        email.sendKeys(getCellContent(2, emptyLicenceKeyRowNumber));
        Thread.sleep(1000);
        WebElement countryCode = webElement("VAR_COUNTRYCODE");
        countryCode.clear();
        countryCode.sendKeys(getCellContent(3, emptyLicenceKeyRowNumber));
        Thread.sleep(1000);
        WebElement phoneNumber = webElement("VAR_PHONENUMBER");
        phoneNumber.clear();
        phoneNumber.sendKeys(getCellContent(4, emptyLicenceKeyRowNumber));
        Thread.sleep(1000);
        List<WebElement> allUserTypes = driver.findElements(By.xpath("//*[@id='add_user']/div/div[5]/div[1]/div/div/div/div/label"));
        for (WebElement ele : allUserTypes) {
            String userType = ele.getText();
            String user= getCellContent(5, emptyLicenceKeyRowNumber);
            if (userType == user) {
                ele.click();
                break;
            }
        }
        Thread.sleep(1000);
        List<WebElement> allCountry = driver.findElements(By.xpath("//*[@id='add_user']/div/div[5]/div[2]/div/div/div/label"));
        for (WebElement ele : allCountry) {
            String country = ele.getText();
            String cntry= getCellContent(6, emptyLicenceKeyRowNumber);
            if (country == cntry) {
                ele.click();
                break;
            }
        }
        Thread.sleep(1000);
        WebElement organization = webElement("VAR_ORGANIZATION");
        organization.clear();
        organization.sendKeys(getCellContent(7, emptyLicenceKeyRowNumber));
        Thread.sleep(1000);
        List<WebElement> allDomains = driver.findElements(By.xpath("//*[@id='add_user']/div/div[7]/div[2]/div/div/div/label"));
        for (WebElement ele : allDomains) {
            String domain = ele.getText();
            String dmn= getCellContent(8, emptyLicenceKeyRowNumber);
            if (domain == dmn) {
                ele.click();
                break;
            }
        }
        Thread.sleep(1000);
        WebElement licenceKey = webElement("VAR_LICENCEKEY");
        licenceKey.click();
        Thread.sleep(1000);
        if (driver.findElement(By.className("disabled")) != null) {
            testresultdata.put("14", new Object[] { 13d, "Add User","Add User without generating licence key","Should not be possible to add user","Should not be possible to add user", "Pass" });
        }
    } catch (Exception e) {
        if (driver.findElement(By.className("disabled")) == null) {
            testresultdata.put("14", new Object[] { 13d, "Add User","Add User without generating licence key","Should not be possible to add user", "Possible to add user","Fail", e.getMessage() });
        }
    }
}


/* Add user without entering first name */

@Test(priority=1)
public void addUserWithOutFirstName() throws BiffException, IOException,InterruptedException {

    try {
        int successfullLoginRowNumber = 6;
        Thread.sleep(2000);
        WebElement username = webElement("VAR_EMAIL");
        username.clear();
        username.sendKeys(getCellContent(0, successfullLoginRowNumber));
        Thread.sleep(2000);
        WebElement password = webElement("VAR_PASSWORD");
        password.clear();
        password.sendKeys(getCellContent(1, successfullLoginRowNumber));
        Thread.sleep(2000);
        WebElement signInButton = webElement("VAR_SIGNINBUTTON");
        signInButton.click();
        Thread.sleep(2000);
        input(properties.getProperty("VAR_ADDUSERDETAILS"));
        int emptyFirstNameRowNumber = 2;
        WebElement addUser = webElement("VAR_ADDUSER");
        addUser.click();
        Thread.sleep(2000);
        WebElement firstName = webElement("VAR_FIRSTNAME");
        firstName.clear();
        firstName.sendKeys(getCellContent(0, emptyFirstNameRowNumber));
        Thread.sleep(1000);
        WebElement lastName = webElement("VAR_LASTNAME");
        lastName.clear();
        lastName.sendKeys(getCellContent(1, emptyFirstNameRowNumber));
        Thread.sleep(1000);
        WebElement email = webElement("VAR_ADDUSEREMAIL");
        email.clear();
        email.sendKeys(getCellContent(2, emptyFirstNameRowNumber));
        Thread.sleep(1000);
        WebElement countryCode = webElement("VAR_COUNTRYCODE");
        countryCode.clear();
        countryCode.sendKeys(getCellContent(3, emptyFirstNameRowNumber));
        Thread.sleep(1000);
        WebElement phoneNumber = webElement("VAR_PHONENUMBER");
        phoneNumber.clear();
        phoneNumber.sendKeys(getCellContent(4, emptyFirstNameRowNumber));
        Thread.sleep(1000);
        List<WebElement> allUserTypes = driver.findElements(By.xpath("//*[@id='add_user']/div/div[5]/div[1]/div/div/div/div/label"));
        for (WebElement ele : allUserTypes) {
            String userType = ele.getText();
            String user= getCellContent(5, emptyFirstNameRowNumber);
            if (userType.equals(user)) {
                ele.click();
                break;
            }
        }
        Thread.sleep(1000);
        List<WebElement> allCountry = driver.findElements(By.xpath("//*[@id='add_user']/div/div[5]/div[2]/div/div/div/label"));
        for (WebElement ele : allCountry) {
            String country = ele.getText();
            String cntry= getCellContent(6, emptyFirstNameRowNumber);
            if (country.equals(cntry)) {
                ele.click();
                break;
            }
        }
        Thread.sleep(1000);
        WebElement organization = webElement("VAR_ORGANIZATION");
        organization.clear();
        organization.sendKeys(getCellContent(7, emptyFirstNameRowNumber));
        Thread.sleep(1000);
        List<WebElement> allDomains = driver.findElements(By.xpath("//*[@id='add_user']/div/div[7]/div[2]/div/div/div/label"));
        for (WebElement ele : allDomains) {
            String domain = ele.getText();
            String dmn= getCellContent(8, emptyFirstNameRowNumber);
            if (domain.equals(dmn)) {
                ele.click();
                break;
            }
        }
        Thread.sleep(1000);
        WebElement licenceKey = webElement("VAR_LICENCEKEY");
        licenceKey.click();
        Thread.sleep(1000);
        if (driver.findElement(By.xpath("//*[@id='add_user']/div/div[9]/div/div")) != null) {
            testresultdata.put("14", new Object[] { 13d, "Add User","Add User without entering first name","Should not be possible to add user","Should not be possible to add user", "Pass" });
        }
    } catch (Exception e) {
        if (driver.findElement(By.xpath("//*[@id='add_user']/div/div[9]/div/div")) == null) {
            testresultdata.put("14", new Object[] { 13d, "Add User","Add User without entering first name","Should not be possible to add user", "Possible to add user","Fail", e.getMessage() });
        }
    }
}

If you are using Eclipse > goto Project > Clean > select your project. 如果您正在使用Eclipse> goto Project> Clean>选择您的项目。

Then select your testng.xml > run as TestNG Suite. 然后选择您的testng.xml>作为TestNG Suite运行。

Instead of priority, you should try to use dependsOnMethods . 您应该尝试使用dependsOnMethods而不是优先级。

Following code will help you : 以下代码将帮助您:

import org.testng.Assert;
import org.testng.annotations.Test;

public class DependencyAnnotation {

   @Test(dependsOnMethods = { "addUserWithOutFirstName" })
   public void addUserWithOutGeneratingLicenceKey() {
      System.out.println("Inside addUserWithOutGeneratingLicenceKey");
    }

   @Test
   public void addUserWithOutFirstName() {
      System.out.println("This is addUserWithOutFirstName");
   }
}

Then check for testng.xml file code 然后检查testng.xml文件代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1">
   <test name="test1">
      <classes>
         <class name="DependencyAnnotation" />
      </classes>
   </test>
</suite>

Hope it will help you. 希望它会对你有所帮助。

Don't use priority=0. 不要使用priority = 0。

For me @Test(priority=1) and @Test(priority=2) work as intended. 对我来说,@ Test(优先级= 1)和@Test(优先级= 2)按预期工作。

Then if you want further control of your tests you can add the dependsOnMethods combined with the priority 然后,如果您想进一步控制测试,可以将dependsOnMethods与优先级相结合

@Test (priority=1)
public void method1(){
}

@Test (priority=2, dependsOnMethods="method1")
public void method2(){
}

@Test (priority=3, dependsOnMethods="method2")
public void method3(){
}

@Test (priority=4)
public void method4(){
}

In the example above all methods 1-4 will be executed sequentially. 在上面的示例中,将顺序执行所有方法1-4。 If for some reason methods 1 fails, then methods 2 will be skipped (as it depends on Method #1). 如果由于某种原因方法1失败,则将跳过方法2(因为它取决于方法#1)。 Similarly method #3 will be skipped as well (as it depends on #2). 类似地,也将跳过方法#3(因为它取决于#2)。 Finally, in the event of #1 failing, the next method to be executed will be 4. 最后,如果#1失败,下一个要执行的方法将是4。

  • 1-FAIL 1-FAIL
  • 2-SKIPPED 2- SKIPPED
  • 3-SKIPPED 3跳过
  • 4-RUN 4-RUN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM