我想自动生成无效且有效的凭据

时间:2016-11-11 05:54:32

标签: java selenium

我正在使用页面工厂设计模式为Web应用程序进行Selenium自动化。现在,我想为登录页面自动执行有效的,无效的无效,无效的有效凭证。怎么回事?

我的完整代码是

package com.docmgr.Pages;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.support.CacheLookup;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.How;
    import org.openqa.selenium.WebElement;
    public class LoginPage
    {
        WebDriver driver;
        public LoginPage(WebDriver driver)
        {
            this.driver=driver;
        }
        @FindBy(how=How.NAME,using="username")
        @CacheLookup
        WebElement username;
        @FindBy(how=How.NAME,using="password")
        @CacheLookup
        WebElement password;
        @FindBy(how=How.CLASS_NAME,using="button")
        @CacheLookup
        WebElement button;
        @FindBy(how=How.LINK_TEXT,using="Forgot Password")
        @CacheLookup
        WebElement fp;
        public void login_Doc(String uid,String pas)
        {
            username.sendKeys(uid);
            password.sendKeys(pas);
            button.click();
        }
    }


    package com.docmgr.TestCases;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.support.PageFactory;
    import org.testng.annotations.Test;

    import com.docmgr.Pages.LoginPage;

    import Helper.BrowserFactory;

    public class LoginTest
    {
        @Test
        public void chechValidUser()
        {
            System.setProperty("firefox.webdriver.marionette","pathToGeckodriver");
            WebDriver driver=BrowserFactory.startBrowser("firefox","54.68.159.876/docmgr");
            LoginPage login=PageFactory.initElements(driver,LoginPage.class);
            login.login_Doc("jgsdg","123");
        }
    }

    package Helper;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class BrowserFactory
    {
        static WebDriver driver;
        public static WebDriver startBrowser(String browsName,String url)
        {
            if(browsName.equals("firefox"))
            {
                driver=new FirefoxDriver();
            }

            driver.manage().window().maximize();
            driver.get(url);
            return driver;
        }
    }

2 个答案:

答案 0 :(得分:3)

传递login_Doc方法的额外参数,表示授予用户名和密码有效。请看下面的例子。

public void login_Doc(String uid,String pas,boolean isValidCredential)
{
    username.sendKeys(uid);
    password.sendKeys(pas);
    button.click();
    if(isValidCredential == true){
      // check if user is logged in successfully and click on logout button
   } else {
      //check appropriate error message is displayed
    }
}

并将login.login_Doc方法称为

  login.login_Doc("admin","admin",true); //valid credential
  login.login_Doc("admin","admin123",false); //invalid credential

答案 1 :(得分:1)

您的测试将如下所示:

public class LoginTest
{
@Test
public void chechValidUser()
{
    login.login_Doc("valid","valid");
}

@Test
public void chechValidInvalidUser()
{
    login.login_Doc("valid","invalid");
    String bodyText = driver.findElement(By.tagName("body")).getText();
    Assert.assertTrue("Text not found!", bodyText.contains("Invalid Password"));
}

@Test
public void chechInvalidInvalidUser()
{
    login.login_Doc("invalid","invalid");
    String bodyText = driver.findElement(By.tagName("body")).getText();
    Assert.assertTrue("Text not found!", bodyText.contains("Invalid Username"));
}
}

如果您将登录功能更改为:

public void login_Doc(String uid,String pas)
{
    System.setProperty("firefox.webdriver.marionette","pathToGeckodriver");
    WebDriver driver=BrowserFactory.startBrowser("firefox","54.68.159.876/docmgr");
    LoginPage login=PageFactory.initElements(driver,LoginPage.class);
    username.sendKeys(uid);
    password.sendKeys(pas);
    button.click();
 }