全局等待和全局启动driver

package startAndWait;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import pages.LoginPage;

import java.awt.AWTException;
import java.awt.Robot;

import org.openqa.selenium.JavascriptExecutor;


public class StartChormeAndWait {    
    
    //定义静态变量driver
    public static WebDriver driver;    
    
    //初始化浏览器
    public  static void startChrome(WebDriver webDriver){
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--test-type");  
        System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
        driver= new ChromeDriver(options);
        driver=webDriver;    
        
    }
    public void WaitForPageLoaded(WebDriver driver) throws AWTException {
        PageFactory.initElements(driver, this);
        this.driver=driver;
        }
        
    //等待页面加载时间
    public static void waitForPage(WebDriver myDriver)throws Exception
    {
        ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor) driver).executeScript(
                "return document.readyState").toString().equalsIgnoreCase("complete");
            }
        };
        Wait<WebDriver> wait = new WebDriverWait(myDriver, 30);
        try {
            wait.until(expectation);
        } catch (Exception e) {
            System.out.println("Timeout waiting for Page Load Request to complete.");
        }
        //System.out.println("before sync: " + Global.getDriver().executeScript("return document.readyState").toString());
        //Thread.sleep(5000);
        //System.out.println("after sync: " + Global.getDriver().executeScript("return document.readyState").toString());
      Thread.sleep(500);
    }                                                                                                                                                                               

}

 

posted @ 2016-08-18 11:28  logo_mm  阅读(189)  评论(0编辑  收藏  举报