zoukankan      html  css  js  c++  java
  • begin

    PageObject模式:

    /**
     * Takes a username and password, fills out the fields, and clicks "login".
     * @return An instance of the AccountPage
     */
    public AccountPage loginAsUser(String username, String password) {
      WebElement loginField = driver.findElement(By.id("loginField"));
      loginField.clear();
      loginField.sendKeys(username);
    
      // Fill out the password field. The locator we're using is "By.id", and we should
      // have it defined elsewhere in the class.
      WebElement passwordField = driver.findElement(By.id("password"));
      passwordField.clear();
      passwordField.sendKeys(password);
    
      // Click the login button, which happens to have the id "submit".
      driver.findElement(By.id("submit")).click();
    
      // Create and return a new instance of the AccountPage (via the built-in Selenium
      // PageFactory).
      return PageFactory.newInstance(AccountPage.class);
    }

    样例:

    public class GoogleSearchPage extends LoadableComponent<GoogleSearchPage> {
      private final WebDriver driver;
      private GSPFluentInterface gspfi;
    
      public class GSPFluentInterface {
        private GoogleSearchPage gsp;
    
        public GSPFluentInterface(GoogleSearchPage googleSearchPage) {
            gsp = googleSearchPage;
        }
    
        public GSPFluentInterface clickSearchButton() {
            gsp.searchButton.click();
            return this;
        }
    
        public GSPFluentInterface setSearchString( String sstr ) {
            clearAndType( gsp.searchField, sstr );
            return this;
        }
      }
    
      @FindBy(id = "gbqfq") private WebElement searchField;
      @FindBy(id = "gbqfb") private WebElement searchButton;
      public GoogleSearchPage(WebDriver driver) {
        gspfi = new GSPFluentInterface( this );
        this.get(); // If load() fails, calls isLoaded() until page is finished loading
        PageFactory.initElements(driver, this); // Initialize WebElements on page
      }
    
      public GSPFluentInterface withFluent() {
        return gspfi;
      }
    
      public void clickSearchButton() {
        searchButton.click();
      }
    
      public void setSearchString( String sstr ) {
        clearAndType( searchField, sstr );
      }
    
      @Override
      protected void isLoaded() throws Error {
        Assert.assertTrue("Google search page is not yet loaded.", isSearchFieldVisible() );
      }
    
      @Override
      protected void load() {
        if ( isSFieldPresent ) {
          Wait<WebDriver> wait = new WebDriverWait( driver, 3 );
          wait.until( visibilityOfElementLocated( By.id("gbqfq") ) ).click();
        }
      }
    }
    

     Selenium官网:

    https://www.w3.org/TR/webdriver/#dependencies

    https://seleniumhq.github.io/docs/

    Selenium_doc

     https://seleniumhq.github.io/selenium/docs/api/java/index.html?overview-summary.html

  • 相关阅读:
    pm2日志切割
    PM2常用命令
    Linux安装nodejs
    npm 修改源地址
    nodejs 生成验证码
    shell脚本解析json文件
    mysql添加用户并赋予权限命令
    Redis 配置密码
    JavaScript也是黑客技术?
    angular和vue的对比学习之路
  • 原文地址:https://www.cnblogs.com/wldan/p/10527046.html
Copyright © 2011-2022 走看看