java - Selenium TestNG java - 参数太多

标签 java selenium parameters refactoring pageobjects

我正在使用Java语言中的TestNG做Selenium数据驱动框架

我有 pageObject Login_Page.java 来存储页面上所有可用的定位器。

然后我将 appModules Login_Action.java 作为重复登录流程的常用函数。

正如您所注意到的,ExecuteLoginAction() 中的步骤是相同的​​,但由于需要测试用例输入,因此具有不同数量的参数。在这种情况下如何优化代码?

在我的测试脚本中,我将调用 Login_Action.ExecuteLoginAction(many...parameters)

如何避免 MyTestScript_001Test()Login_Action.ExecuteLoginAction() 中出现这么长的参数列表

pageObjects Login_Page.java

package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

    public class Login_Page extends BaseClass {

        private static WebElement element = null;

        public Login_Page(WebDriver driver){
                super(driver);
        }

        public static WebElement txt_enterUsername() throws Exception{
            try{
                element = driver.findElement(By.name("username"));
            }catch (Exception e){
                throw(e);
                }
            return element;
        }

        public static WebElement txt_enterPassword() throws Exception{
            try{
                element = driver.findElement(By.name("password"));
            }catch (Exception e){
                throw(e);
                }
            return element;
        }

        public static WebElement btn_clickLoginBtn() throws Exception{
            try{
                element = driver.findElement(By.name("loginBtn"));
            }catch (Exception e){               
                throw(e);
                }
            return element;
        }                                           
    }

appModules Login_Action.java

package appModules;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.testng.Reporter;

import pageObjects.Login_Page;

    public class Login_Action {     

        public static void ExecuteLoginAction(WebDriver driver, String ColTestCaseName, String ColUsername, String ColPassword, 
                String ColFirstName, String ColLastName, String ColAddress, String ColCountry, String ColGender) throws Exception{          

            // Click Login link
            Home_Page.lnk_clickLoginBtn().click();

            // Enter text for Username       
            Login_Page.txt_enterUsername().sendKeys(ColUsername);

            // Enter text for Password 
            Login_Page.txt_enterPassword().sendKeys(ColPassword);

            // Click Login submit button
            Login_Page.btn_clickLoginSubmitBtn().click();

        }

        public static void ExecuteLoginAction(WebDriver driver, String ColTestCaseName, String ColUsername, String ColPassword, 
                String ColFirstName, String ColLastName, String ColAddress) throws Exception{           

            // Click Login link
            Home_Page.lnk_clickLoginBtn().click();

            // Enter text for Username       
            Login_Page.txt_enterUsername().sendKeys(ColUsername);

            // Enter text for Password 
            Login_Page.txt_enterPassword().sendKeys(ColPassword);

            // Click Login submit button
            Login_Page.btn_clickLoginSubmitBtn().click();

        }

    }

主测试脚本 (MyTestScript_001)

@Test(dataProvider="MyTestScript_001Data")
    public void MyTestScript_001Test(String ColTestCaseName, String ColUsername, String ColPassword, 
                String ColFirstName, String ColLastName, String ColAddress) throws Exception{


            // Login to web application
            Login_Action.ExecuteLoginAction(driver, ColTestCaseName, ColUsername, ColPassword, 
                 ColFirstName,  ColLastName,  ColAddress,  ColCountry,  ColGender);                                             

            // Enter First Name
            UpdateProfile_Page.txtbx_enterFirstName().sendKeys(ColFirstName);

            // Enter Last Name
            UpdateProfile_Page.txtbx_enterLastName().sendKeys(ColLastName);

            Search_Action.ExecuteSearchAction(driver, ColTestCaseName, ColUsername, ColPassword, 
                 ColFirstName,  ColLastName,  ColAddress,  ColCountry,  ColGender);

主测试脚本 (MyTestScript_002)

Main Test Script (MyTestScript_002)

@Test(dataProvider="MyTestScript_002Data")
public void MyTestScript_001Test(String ColTestCaseName, String ColUsername, String ColPassword, 
            String ColFirstName, String ColLastName, String ColAddress) throws Exception{


        // Login to web application
        Login_Action.ExecuteLoginAction(driver, ColTestCaseName, ColUsername, ColPassword, 
             ColFirstName, ColLastName, ColAddress);                                                                    

        // Enter text for Address
        UpdateProfile_Page.txtbx_enterAddress().sendKeys(ColAddress);

非常感谢您的建议!

最佳答案

发送这么多参数没有意义,我会发送一个对象:

public class UserInfo {
    private String colTestCaseName;
    private String coldUserName;
    private String colPassword;
    public String getColTestCaseName() {
        return colTestCaseName;
    }
    public void setColTestCaseName(String colTestCaseName) {
        this.colTestCaseName = colTestCaseName;
    }
    public String getColdUserName() {
        return coldUserName;
    }
    public void setColdUserName(String coldUserName) {
        this.coldUserName = coldUserName;
    }
    public String getColPassword() {
        return ColPassword;
    }
    public void setColPassword(String colPassword) {
        this.colPassword = colPassword;
    }
}

调用方法:

UserInfo userInfo = new UserInfo();
//Add credentials
userInfo.setColUsername("user1");
....
Login_Action.ExecuteLoginAction(driver, userInfo);

关于java - Selenium TestNG java - 参数太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28449878/

相关文章:

java - 更改另一个 java 文件中定义的最终变量

java - 即使主类已存在,也未找到

java - Hadoop如何实现组比较器?

javascript - 在 FireFox 和 IE 中使用 Selenium WebdriverJS 时出现问题

java - 我在将 View 显示为 ToS 的弹出窗口时遇到问题

python - 无法找到元素 selenium webdriver

c# - Selenium C# try/catch 请提供帮助

java - 接受void的通用参数类型

button - Jenkins - 这可能吗?为基于参数的作业设置多个构建按钮

java - 在 onTouchListener 中设置宽度