Selenium:无法使用数据提供程序识别对象

时间:2016-04-19 10:54:25

标签: java selenium-webdriver

在这个领域的初学者,感谢你对这个问题的帮助!

我正在尝试编写路径

登录 创建新用户 退出

登录方法正常 创建用户依赖于Login方法,使用数据提供程序和登录后创建新用户

问题是在记录创建用户类中的元素/对象后未识别。我尝试过使用不同的元素查找器和浏览器。感谢您的帮助

项目结构是 项目 包1  Logindataprovider.class  newaccountDataProvider.class 包2  LoginpageObjects.class  NewAccountPageObjects.class 包3  LoginTestscripts.class  NewAccountTestScript.class

LoginTestscripts- Class

LoginPageObjects LgnObj = new LoginPageObjects();
@Test(groups = { "Logingroup1" }, dataProvider = "LoginCred", dataProviderClass = LoginDataProviders.class)
public void mytest(String DDUname, String DDpwd,String ExpLoginPage, int Browsertype) {

        LgnObj.setBrowser(Browsertype);

        Logger log = Logger.getLogger(LoginDataProviders.class);
        log.info("Login To application method");

        LgnObj.init();

        // Login page assertion actual values
        String LoginPageAssertactual = LgnObj.LoginPageAssertionActual();

        //Login page assertion
        Assert.assertEquals(LoginPageAssertactual, ExpLoginPage);

        //login to application
        LogginCommonProcess(DDUname, DDpwd, LgnObj);

    }

    public void LogginCommonProcess(String DDUname, String DDpwd,
            LoginPageObjects LgnObj) {

        LgnObj.enterUserName(DDUname);
        LgnObj.enterPassword(DDpwd);

        LgnObj.Loginbtn();
        LgnObj.enterPIN();
        LgnObj.CompanyName();

    }

Logindataprovider- Class
public class LoginPageObjects {

    WebDriver driver;

    public void setBrowser(int type) {
        if (type == 1) {
            driver = new FirefoxDriver();
        } else if (type == 2) {
            String path = ClassLoader.getSystemResource("chromedriver.exe")
                    .getPath();
            System.setProperty("webdriver.chrome.driver", path);
            driver = new ChromeDriver();
        } else if (type == 3) {
            String path = ClassLoader.getSystemResource("IEDriverServer.exe")
                    .getPath();
            System.setProperty("webdriver.ie.driver", path);
            driver = new InternetExplorerDriver();
        }
    }

    public void init() {
        driver.manage().window().maximize();
        driver.navigate().to("https://test2.qatest.com/Login.aspx");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    }

    public String LoginPageAssertionActual() {
        String LoginPageAssertionact = driver.findElement(By.tagName("h1"))
                .getText();
        return LoginPageAssertionact;
    }

    public void enterUserName(String DDUname) {

        driver.findElement(By.id("ctl00_MainContent_usernameField")).sendKeys(
                DDUname);
    }

    public void enterPassword(String DDpwd) {
        driver.findElement(By.id("ctl00_MainContent_passwordField")).sendKeys(
                DDpwd);
    }

New User- page object
public class NewUser {

    WebDriver driver;

    public void setup() {
        System.out.println("Start here");
        System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer.exe");
        driver = new InternetExplorerDriver();
        driver.manage().window().maximize();

        driver.navigate().to("https://test2.directorsdesk.com/Login.aspx");

        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    }

    public void setBrowser(int type) {
        if (type == 1) {
            driver = new FirefoxDriver();
        } else if (type == 2) {
            String path = ClassLoader.getSystemResource("chromedriver.exe")
                    .getPath();
            System.setProperty("webdriver.chrome.driver", path);
            driver = new ChromeDriver();
        } else if (type == 3) {
            String path = ClassLoader.getSystemResource("IEDriverServer.exe")
                    .getPath();
            System.setProperty("webdriver.ie.driver", path);
            driver = new InternetExplorerDriver();
        } else if (type == 4) {
            DesiredCapabilities cap = DesiredCapabilities.chrome();
            cap.setBrowserName("chrome");
            cap.setVersion("38");
            cap.setPlatform(Platform.WINDOWS);
            try {
                driver = new RemoteWebDriver(new URL(
                        "http://127.0.0.1:4444/wd/hub"), cap);
            } catch (MalformedURLException e) {
                // log erro unable to access remote system, running in local
                // firefox
                driver = new FirefoxDriver();

            }
        }
    }

    **public void ManageSystem() {
        driver.findElement(By.partialLinkText("Manage System")).click();
        driver.findElement(By.linkText("Manage System")).click();

    }**

public class NewAccount {

    @Test(dataProvider = "UserDetails", dataProviderClass = LoginDataProviders.class, dependsOnGroups = "Logingroup1")
    public void CreateNewAccount(String Emailid, String Firstname,
            String Lastname, String PrimaryEmailid, int browserType) {

        NewUser NewUserObj = new NewUser();
        NewUserObj.setBrowser(browserType);
        **NewUserObj.ManageSystem();** // Fails here
        NewUserObj.UserAccounts();
        NewUserObj.NewUserAccount();
        NewUserObj.Emailid(Emailid);
        NewUserObj.UserFirstname(Firstname);
        NewUserObj.UserLastname(Lastname);
        NewUserObj.PrimaryEmai(PrimaryEmailid);
        NewUserObj.SaveAccountSettings();

        LoginPageObjects LgnPgObj = new LoginPageObjects();
        LgnPgObj.Logout();
        LgnPgObj.closeBrowser();

    }

2 个答案:

答案 0 :(得分:0)

你试过这个:

public void entityFieldIsEntered(String field, String value)  {
    WebElement valueElem = new SharedDriver().findElement(By.xpath("(.//*[text()='" + field
            + ":'])[2]/parent::*/span/input"));
    valueElem.sendKeys(new CharSequence[] { value });

}

假设您在html中使用输入包装器。此代码在屏幕上搜索文本标签,然后转到其父级并导航到输入字段。

答案 1 :(得分:0)

如果要在一次测试中使用多个页面对象,则需要在这些页面对象之间共享相同的驱动程序。 尝试创建一个单独的setupDriver类,并让每个页面对象获取要构造的驱动程序参数。 在每个测试开始时设置驱动程序,然后在创建每个对象时传入驱动程序。