2

我正在尝试为传递用户名和密码等参数的登录测试脚本创建一个函数。我是新手,请详细说明。这是我写的代码——

public void portalLogin(String Username, String Password){

driver.get(URL);
driver.findElement(By.id("UserName")).clear();
driver.findElement(By.id("UserName")).sendKeys(Username);
driver.findElement(By.id("Password")).clear();
driver.findElement(By.id("Password")).sendKeys(Password);
driver.findElement(By.cssSelector("input.submitBtn")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} 

我只想将这个奇怪的函数调用到另一个测试类,这样我就不必一遍又一遍地编写它。请帮忙!

4

4 回答 4

3

您说您正在尝试使用页面对象模式。这就是我认为您正在寻找的内容:

public class LogonPage {
@FindBy(id="UserName")
public WebElement userNameField;
@FindBy(id = "Password")
public WebElement passwordField;
@FindBy(css = "input.submitBtn")
public WebElement submitButton;

public LogonPage(WebDriver driver){
    PageFactory.initElements(driver, this);
}
public void portalLogin(String username, String password){ //Only call this when you are already on the logon page
        userNameField.clear();
        userNameField.sendKeys(username);
        passwordField.clear();
        passwordField.sendKeys(password);
        submitButton.click();
        //Waiting should also be done after the function
}

}

然后,您必须在主代码中执行此操作:

LogonPage page = new LogonPage(driver);
page.doLogin(username, password);
于 2013-07-23T19:01:40.307 回答
0
public static WebDriver LoginPage(String URL, CharSequence UName, CharSequence Pwd) throws Exception 
        {
          driver.get(URL);
          driver.findElement(By.id("username")).clear();
          driver.findElement(By.id("username")).sendKeys(UName);
          driver.findElement(By.id("password")).clear();
          driver.findElement(By.id("password")).sendKeys(Pwd);

          // [click on link or button after which popup window will appear. ] 
         driver.findElement(By.name("login")).click();
    //   System.out.println("URL of the page before - switchingTo: " + driver.getCurrentUrl());  
         Thread.sleep(2000);

          //Switch to newly opened window and get the page titele    
               for(String CurrentWindowHandle : driver.getWindowHandles())
                   {   driver.switchTo().window(CurrentWindowHandle);     
                   }  

            System.out.println("URL of the page after - switchingTo: " + driver.getCurrentUrl()); 
             Thread.sleep(3000);
             return driver;
        }        

然后在驱动程序脚本中它可以被称为 LoginPage(vURL,vUserName,vPassword);

于 2015-01-16T08:34:37.370 回答
0

设置超时并首先导航到 URL。在应用程序中打开登录页面。

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);    
driver.get(URL);

    public void doLogin(String Username, String Password)
{
    driver.findElement(By.id("UserName")).clear();
    driver.findElement(By.id("UserName")).sendKeys(Username);
    driver.findElement(By.id("Password")).clear();
    driver.findElement(By.id("Password")).sendKeys(Password);
    driver.findElement(By.cssSelector("input.submitBtn")).click();
    }

如果您需要更多帮助,请告诉我...

于 2013-07-22T16:11:55.750 回答
0

这是 JUnit 参数化测试的示例。我想这就是你所要求的。

@RunWith(Parameterized.class)
public class DataProviderTest extends WebDriverUtils {

    private static String testName, searchString, ddMatch;

    public DataProviderTest( String tName, String sString, String dMatch ) {
        testName = tName;
        searchString = sString;
        ddMatch = dMatch;
    }

    @Before
    public void setUp() {   
        System.out.println("setUp");
    }

    @Parameters(name = "{0}: {1}: {2}")
    public static List<String[]> loadParams() {
        File tFile = loadGradleResource( System.getProperty("user.dir") + separator +  "build" +
                separator + "resources" + separator +  "test" + separator + "testdata2.csv" );
        List<String[]> rows = null;
        if ( tFile.exists() ) {
            CSVReader reader = null;
            try {
                reader = new CSVReader( new FileReader( tFile ), ',' );
                rows = reader.readAll();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }   
        }
        //String[][] csvMatrix = rows.toArray(new String[rows.size()][]);
        System.out.println("Finished loadParams()");
        return rows;
    }  

    @Test
    public void testParams() {
        System.out.println("Param '{}' being run...", testName );
        System.out.println("Search string: " + searchString );
        System.out.println("ddMatch: " + ddMatch );
        System.out.println("Test '{}' is done.", testName );
    }

    @After
    public void cleanUp() {
        System.out.println("Finished cleanUp");
    }

}
于 2013-07-23T20:53:39.127 回答