2017-11-08 6 views
1

나는이 같은 셀레늄에 명시 적으로 대기를 구성하는거야 :Selenium에서 페이지 팩토리를 사용하는 동안 명시 적으로 대기하는 방법은 무엇입니까?

WebDriverWait = new WebDriverWait(driver,30); 

WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(locator)); 

문제는 내 클래스에서 드라이버를 필요가 없다는 것입니다, 나는 PageFactory을 사용하기 때문에,없는 생성자를 시험에 수업 :

MyClass myform = PageFactory.InitElements(driver, MyClass.class) 

이 경우 명시 적 대기를 구성하는 좋은 결정은 무엇입니까?

+0

찾을 수있는 책 셀레늄 WebDriver 실용 가이드의 예를 모델로했다 ...'PageObject 클래스의 생성자에서 다음 'MyClass myform = new MyClass (드라이버);'를 실행하십시오. – SiKing

답변

4

의도 한대로 PageFactory를 사용하고 명시 적 대기를 사용하려는 클래스에 대한 생성자가있는 것이 좋습니다. 스크립트와 페이지 객체를 분리하면 나중에 더 쉽게 작업 할 수 있습니다.

public class MyClass { 

    WebDriverWait wait; 
    WebDriver driver; 
    @FindBy(how=How.ID, id="locatorId") 
    WebElement locator; 

    // Construct your class here 
    public MyClass(WebDriver driver){ 
     this.driver = driver; 
     wait = new WebDriverWait(driver,30); 
    } 

    // Call whatever function you want to create 
    public void MyFunction(){ 
     wait.until(ExpectedConditions.presenceOfElementLocated(locator)); 
     // Perform desired actions that you wanted to do in myClass 
    } 

그런 다음 테스트 케이스에서 테스트를 수행하기 위해 코드를 사용하십시오. 귀하의 예제에서 대기는 페이지 내에 포함되어 있습니다.

public class MyTestClass { 
    public static void main (string ... args){ 
     WebDriver driver = new FireFoxDriver(); 
     MyClass myForm = PageFactory.initElements(driver,Myclass.class); 
     myForm.MyFunction(); 
    } 
} 

이 예는`PageFactory.InitElements을 넣어 여기 here

+0

그것은 작동합니다. 고맙습니다! – dmytrocx75