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

  • 相关阅读:
    js、css等文件引入空白问题
    Vue 组件 data为什么是函数
    安装Node,创建vue项目,运行及打包
    JQuery移除事件
    百度地图定位
    移动端导航过多,点击导航左右滚动回弹
    移动端开发模板
    移动端左右滑动导航
    使用‘圣杯布局’、‘双飞翼布局’来解释自适应的三栏水平布局
    css实现三角箭头
  • 原文地址:https://www.cnblogs.com/wldan/p/10527046.html
Copyright © 2011-2022 走看看