首先在Selenium TestNG中执行了错误的测试

时间:2015-09-07 06:14:35

标签: selenium selenium-webdriver testng

在下面的脚本中,我想首先运行Test'Login',然后运行'CreateCompany'。但是每当我尝试运行脚本时,控件首先会进入'CreateCompany'测试。并且由于用户尚未登录,因此脚本执行失败。请有人告诉我下面的脚本有什么问题。

package MyPackage;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import MyPackage.PageObjects.CompanyListing;
import MyPackage.PageObjects.LoginPage;

public class SetUpCompany {
    public static WebDriver driver;
    public String BaseURL=LoginPage.BaseURL();
    @BeforeClass
    public static void setup()  {
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
       }  
    //public static WebDriver driver = new FirefoxDriver();
    @Test(dataProvider="UserData")
    void Login(String username, String password, String usertype){      
        //dr.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        if(usertype.equals("SuperAdmin")){          
            String LoginURL=BaseURL+"/Manage";
            driver.get(LoginURL);
            LoginPage.UsernameBox(driver).sendKeys(username);
            LoginPage.PasswordBox(driver).sendKeys(password);
            LoginPage.LoginButton(driver).click();
            //System.out.println("Hello Super admin!");
        }

    }
    @Test(dataProvider="CompanyData")
    void CreateCompany(String FrstNm, String LastNm, String CmpnyNm, String Email, String Phone, String Cell, String Web){
        System.out.println("I'm here");
        String ManageCompanyURL=BaseURL+"/Manage/Company";
        driver.get(ManageCompanyURL);
        CompanyListing.AddCmpnyBtn(driver).click();
    }
    @DataProvider(name="UserData")
    public Object[][] loginData() {
        Object[][] LoginArray = DataReader.getExcelData("E:/DataBase.xls","LoginData");
        return LoginArray;
    }
    @DataProvider(name="CompanyData")
    public Object[][] CompanyInfo() {
        Object[][] CompanyArray = DataReader.getExcelData("E:/DataBase.xls","CompanyInfo");
        return CompanyArray;
    }
    @AfterClass
    public static void teardown()  {
    driver.close();
    driver.quit();
     }
}

2 个答案:

答案 0 :(得分:1)

您所拥有的是依赖测试,其中CreateCompany依赖于登录测试。 你应该使用dependsOnMethods。阅读更多here

答案 1 :(得分:0)

尝试在TestNG中的@Test注释中编写优先级来设置执行顺序。这是怎么做的 -

@Test(priority=1, dataProvider="UserData")
void Login(String username, String password, String usertype){
    //Your login code
}
@Test(priority=2, dataProvider="CompanyData")
void CreateCompany(String FrstNm, String LastNm, String CmpnyNm, String Email, String Phone, String Cell, String Web){
    //Your CreateCompany code
}

More info on priorities in TestNG。希望这会有所帮助。