Я новичек в Selenium и Java…Написал тест (в тяжко читаемом виде), и теперь пытаюсь применить Page Object паттерн.
Собственно вопрос возникает в следйщем : допустим у меня есть какой-то
WebDriverWait waitForOne = new WebDriverWait(driver, 10);
waitForOne.until(ExpectedConditions.presenceOfElementLocated(By.xpath("some_xpath")));
и я хотел бы это вынести из своего теста, и поместить в класс, где я описываю элементы страницы. Подскажите пжлст, как это реализовать(чтобы потом вызывать просто через “обьект.метод”)
Пробовал так:
public void waitForSmth () {
WebDriverWait waitForOne = new WebDriverWait(driver, 10);
waitForOne.until(ExpectedConditions.presenceOfElementLocated(By.xpath("some_xpath")));
}
А потом в тесте вызвать примерно так : firstpage.waitForSmth(); , но вылазит NullPointerException
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:94)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:66)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:40)
at com.webdriver.FirstPage.waiter(FirstPage.java:29)
at Tests.test.oneWayReservation(test.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
package com.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
public class FirstPage {
public WebDriver driver;
public FirstPage(WebDriver driver){
PageFactory.initElements(driver, this);
}
public void waiter(){
WebDriverWait waitForOne = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[@class='gwt-RadioButton oneway-button']/input")));
}
Класс с самим тестом:
package Tests;
import org.junit.*;
import com.webdriver.*;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class test {
public FirefoxDriver driver;
@Before //Create an instance of Delta's instance class and provide the driver
public void setUp(){
driver = new FirefoxDriver();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("some_link");
driver.manage().window().maximize();
}
@Test
public void oneWayReservation(){
//Adding page objects
FirstPage firstPage = new FirstPage(driver);
SecondPage secondPage = new SecondPage(driver);
ThirdPage thirdPage = new ThirdPage(driver);
PaymentPage paymentPage = new PaymentPage(driver);
StatusPage statusPage = new StatusPage(driver);
/************First page************/
firstPage.waiter();
В качестве примера, использую стартовую страницу Яндекса.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class FirstPage {
private WebDriver driver;
public FirstPage(WebDriver driver){
PageFactory.initElements(driver, this);
this.driver = driver;
}
public void waiter(){
WebDriverWait waitForOne = new WebDriverWait(driver, 10);
waitForOne.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='tab-maps']")));
}
}
import org.junit.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test {
private WebDriver driver;
@Before //Create an instance of Delta's instance class and provide the driver
public void setUp(){
driver = new FirefoxDriver();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.yandex.ru");
driver.manage().window().maximize();
}
@Test
public void oneWayReservation(){
//Adding page objects
FirstPage firstPage = new FirstPage(driver);
firstPage.waiter();
}
}