Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为Selenium页面对象模型框架编写通用方法_Java_Selenium - Fatal编程技术网

Java 为Selenium页面对象模型框架编写通用方法

Java 为Selenium页面对象模型框架编写通用方法,java,selenium,Java,Selenium,我遵循页面对象模型,在这个模型中,我将所有的WebElement地址和方法,即id、xpath等放在页面中。 示例:在登录页面上,我们有用户名字段、密码字段和登录按钮。所有这些都将在LoginPage.java上。此页面还包括使用Webelements的方法。因此,从实际的测试用例中,我只是调用页面上定义的方法。现在是我的困境。我有几个测试步骤需要我点击一个元素。 假设我需要点击Link1、Link2和Link3。现在我要做的是为每个步骤编写一个单独的方法,并将它们保留在页面上,如下所示: @F

我遵循页面对象模型,在这个模型中,我将所有的WebElement地址和方法,即id、xpath等放在页面中。 示例:在登录页面上,我们有用户名字段、密码字段和登录按钮。所有这些都将在LoginPage.java上。此页面还包括使用Webelements的方法。因此,从实际的测试用例中,我只是调用页面上定义的方法。现在是我的困境。我有几个测试步骤需要我点击一个元素。 假设我需要点击Link1、Link2和Link3。现在我要做的是为每个步骤编写一个单独的方法,并将它们保留在页面上,如下所示:

@FindBy(id = "someId1")
WebElement link1;

@FindBy(id = "someId2")
WebElement link2;


public void clickOnLink1(){
    link1.click();
}

public void clickOnLink2(){
    link2.click();
}
public void clickOnElement(WebElement element) {
    element.click();
}
测试用例:

clickOnLink1();

clickOnLink2();
因此,如果我有50个步骤需要我点击50个不同的元素,我需要50个不同的方法,这是非常不恰当和低效的。我可以使用下面这样的通用方法:

@FindBy(id = "someId1")
WebElement link1;

@FindBy(id = "someId2")
WebElement link2;


public void clickOnLink1(){
    link1.click();
}

public void clickOnLink2(){
    link2.click();
}
public void clickOnElement(WebElement element) {
    element.click();
}
我可以将我想要点击的元素传递给这个方法,它会帮我完成

不幸的是,正如我之前所说,我遵循页面对象模型,因此所有的WebElement地址(如id)都存在于页面上,而不存在于测试用例中,因此我无法使用此方法,因为我需要通过测试用例中的WebElement

有人能帮我换一种方法吗

多谢各位

public class Login_Page {


private static WebElement element = null;


//username
public static WebElement Textbox_UserName (WebDriver driver)
{

    element = driver.findElement(By.id("username"));

    return element;

}
现在,在测试用例方法中,只需调用以下内容:

@Test 
public void Llogin()
{
 //initiate webdriver 
 //driver.get("url")
 Login_page.Textbox_UserName(driver).sendKeys("username")
}

这样如何,将标识符设置为静态,然后从测试中调用pass。虽然我不会给出建议,因为测试可以更改标识符,所以很难理解测试失败的原因

您的页面对象类

public class GoogleSearch {
    @FindBy(name="q")
    static WebElement searchBox;
    public GoogleSearch(WebDriver driver){
        PageFactory.initElements(driver, this);
        driver.get("https://www.google.com");
    }
    public void enterText(WebElement element, String text){
        element.sendKeys(text);
    }
}
您的测试类应该是这样的

public class GoogleTest {

    WebDriver driver;
    @BeforeMethod
    public void setup(){
        driver = new FirefoxDriver();
    }

    @AfterMethod
    public void tearDown(){
        driver.quit();
    }

    @Test
    public void SearchOnGoogle(){

        GoogleSearch googleSearchPage = PageFactory.initElements(driver, GoogleSearch.class);
        googleSearchPage.enterText(GoogleSearch.searchBox, "selenium webdriver");

    }
}

要进一步优化,您可以在BasePage中使用此enterText方法,并且每个页面对象都可以扩展此BasePage类

您可以使用以下方法:

public class GoogleSearch {
    @FindBy(name="q")
    static WebElement searchBox;
    public GoogleSearch(WebDriver driver){
        PageFactory.initElements(driver, this);
        driver.get("https://www.google.com");
    }
    public void enterText(WebElement element, String text){
        element.sendKeys(text);
    }
}
public class Login_Page {


@FindBy(id = "someId1")
WebElement link1;

@FindBy(id = "someId2")
WebElement link2;

//constructor
public Login_Page(driver) {
    PageFactory.initElements(driver, this);
}

//Generic Click method
public boolean genericClick(WebDriver driver, WebElement elementToBeClicked)
{

    try{

     elementToBeClicked.click();

     return true;
}
catch(Exception e){

     return false;
}

}
现在在测试方法中:

@Test 
public void LoginTest()
{
 Webdriver driver = new FirefoxDriver();
 driver.get("someurl")

 Login_Page lp = new Login_Page(driver);
 lp.genericClick(driver, lp.link1);
}

同样的方法,你可以做任何数量的元素在你的登录页面或任何页面。你能给我们一点信息,为什么有50个不同的链接?链接总是在页面上还是动态的?