如何在页面对象建模中定义动作

时间:2018-08-27 03:31:51

标签: selenium-webdriver

我是硒的新蜂。我试图访问PageObject Modeling中的菜单->子菜单。

我的要求是单击菜单项和动态显示的子菜单项。

我正在经历StackOverflow,在其中一项决议中提到将Actions编写为

      Actions actions = new Actions(driver);
  actions.moveToElement(currentOpenings).build().perform();
  WebDriverWait wait = new WebDriverWait(driver,10); 
  wait.until(ExpectedConditions.elementToBeClickable(ausJobsSubmenu));

但是我不能在构造函数中传递驱动程序。如果我通过构造函数,则在运行此脚本时会遇到问题。错误如下

FAILED: checkLinks java.lang.Error: Unresolved compilation problem: 
The method HomePageLinksClick(WebDriver) in the type HomePageLinksTest is not applicable for the arguments ()

如果我在构造函数中包含WebDriver驱动程序作为参数,则我的页面类不接受该参数作为参数。有人可以帮忙吗?

页面类别

package au.com.sreetechconsulting.Pages;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import au.com.sreetechconsulting.TestCases.HomePageLinksTest;
public class HomePage {
 public WebDriver driver;
 @BeforeTest
 public void openBrowser() {
    System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

    driver = new ChromeDriver(); 

    driver.manage().window().maximize();

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


    driver.get("http://www.sreetechconsulting.com.au/");
}
@Test 
public void checkLinks() {
    HomePageLinksTest linkClick = new HomePageLinksTest(driver);

    linkClick.HomePageLinksClick();

 }
}

测试页类别

package au.com.sreetechconsulting.TestCases;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class HomePageLinksTest {

  @FindBy(css="#header > div.container > div > div.col-md-9.main-nav > nav > ul > li:nth-child(2) > a")
 private WebElement currentOpenings;

  @FindBy(css="#header > div.container > div > div.col-md-9.main-nav > nav > ul > li:nth-child(2) > ul > li:nth-child(1) > a")
  private WebElement ausJobsSubmenu;

  public HomePageLinksTest (WebDriver driver) {
     PageFactory.initElements(driver, this);

  }

  public void HomePageLinksClick() {


   //currentOpenings.click();
    Actions actions = new Actions(driver);
    actions.moveToElement(currentOpenings).build().perform();
    WebDriverWait wait = new WebDriverWait(driver,10); 
    wait.until(ExpectedConditions.elementToBeClickable(ausJobsSubmenu));
  }

}

1 个答案:

答案 0 :(得分:0)

您可以将driver保留为构造函数中的类成员

public class HomePageLinksTest {

    private WebDriver driver;

    public HomePageLinksTest (WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void HomePageLinksClick() {
        //currentOpenings.click();
        Actions actions = new Actions(driver);
        actions.moveToElement(currentOpenings).build().perform();
        WebDriverWait wait = new WebDriverWait(driver,10); 
        wait.until(ExpectedConditions.elementToBeClickable(ausJobsSubmenu));
    }
}

在测试中像这样使用它

@Test 
public void checkLinks() {
    HomePageLinksTest linkClick = new HomePageLinksTest(driver);
    linkClick.HomePageLinksClick();
 }