记录自己看selenium源码的一些收获(五)RemoteWebDriver类

记录这个重要类的成员和方法(它是所有其他浏览器driver类的超类)

成员:这些成员有不同的setter getter方法。

 // Selenium的日志入口
  private static final Logger logger = Logger.getLogger(RemoteWebDriver.class.getName());

// 暂时没有搞清楚这个Level的使用目的,好像是用于正常的log记录
  private Level level = Level.FINE;

// 用来进行错误处理的,比如response里面的错误码和exception相互转换等
  private ErrorHandler errorHandler = new ErrorHandler();

// 用来执行我们发送的各类command,它会用来做转化command到request,并且返回response(会调用上面的ErrorHandler来处理response
  private CommandExecutor executor;

// initSession的时候会用到,指定了浏览器的一些属性(option)
  private Capabilities capabilities;

// 获取和浏览器对话的sessionId
  private SessionId sessionId;

// When a file path on the local machine running this script is entered with WebElement#sendKeys, this file detector will transfer the specified file to the Selenium server's host; the sendKeys command will be updated to use the transfered file's path.

// 翻译过来就是当sendkeys是一个path,并且处理的element的input type为file时,file detector将转化成文件发给host。
  private FileDetector fileDetector = new UselessFileDetector();

//这是一个接口,定义了execute方法,封装WebRemoteDriver.executeScripts方法
  private ExecuteMethod executeMethod;

   
// 将chrome返回的包含Element信息的json数据转换成WebElement类
  private JsonToWebElementConverter converter;

// webdriver.Webdriver将要使用的键盘设备
  private RemoteKeyboard keyboard;

// webdriver.Webdriver将要使用的鼠标设备
  private RemoteMouse mouse;

// 提供 Selenium 日志的一个类
  private Logs remoteLogs;

// 用来存储和处理正在进行的log类
  private LocalLogs localLogs;

内部类:涉及的 内部类子方法可以在源码中查看

RemoteTargetLocator:处理Frame相关的操作,定位frame等

RemoteAlert:处理alert相关的操作

RemoteInputMethodManager:IME handler 处理引擎相关

RemoteNavigation:浏览器上的前进,后退等导航条上的操作

RemoteTimeouts:设置超时时间,包括寻找元素的隐式等待/页面加载/执行脚本

RemoteWindow:处理和窗口有关的操作,比如获得/改变当前窗口的大小


方法:

构造函数:最终都要调用这个方法

  public RemoteWebDriver(CommandExecutor executor, Capabilities desiredCapabilities) {
    this.executor = executor;

    init(desiredCapabilities);

    if (executor instanceof NeedsLocalLogs) {
      ((NeedsLocalLogs)executor).setLocalLogs(localLogs);
    }

    try {
// 这个方法为空,暂时不知道为什么为空
      startClient(desiredCapabilities);
    } catch (RuntimeException e) {
      try {
        stopClient(desiredCapabilities);
      } catch (Exception ignored) {
        // Ignore the clean-up exception. We'll propagate the original failure.
      }

      throw e;
    }

    try {
// service和浏览器建立一个会话
      startSession(desiredCapabilities);
    } catch (RuntimeException e) {
      try {
        quit();
      } catch (Exception ignored) {
        // Ignore the clean-up exception. We'll propagate the original failure.
      }

      throw e;
    }
  }

其他方法:

//打印出caps里面的信息 ChromeDriver: chrome on ANY (aa3d0419301c4f82b0bcfc1dc4a1dd55)
public String toString() {}

// 模拟一系列鼠标等输入设备的操作。
  public void perform(Collection<Sequence> actions) {
    execute(DriverCommand.ACTIONS, ImmutableMap.of("actions", actions));
  }

// 执行Javascript脚本
  public Object executeScript(String script, Object... args) {
    if (!capabilities.is(SUPPORTS_JAVASCRIPT)) {
      throw new UnsupportedOperationException(
          "You must be using an underlying instance of WebDriver that supports executing javascript");
    }

    // Escape the quote marks
    script = script.replaceAll("\"", "\\\"");

    Iterable<Object> convertedArgs = Iterables.transform(
        Lists.newArrayList(args), new WebElementToJsonConverter());

    Map<String, ?> params = ImmutableMap.of(
        "script", script,
        "args", Lists.newArrayList(convertedArgs));

    return execute(DriverCommand.EXECUTE_SCRIPT, params).getValue();
  }

// 带async,加载后续文档元素的过程将和JS的加载与执行并行进行(异步)
  public Object executeAsyncScript(String script, Object... args) {}

// 主page,也就是返回html最上层的frame
   public WebDriver defaultContent() {
      Map<String, Object> frameId = Maps.newHashMap();
      frameId.put("id", null);
      execute(DriverCommand.SWITCH_TO_FRAME, frameId);
      return RemoteWebDriver.this;
    }

// 当前页面活跃的Element
    public WebElement activeElement() {
      Response response = execute(DriverCommand.GET_ACTIVE_ELEMENT);
      return (WebElement) response.getValue();
    }

Cookie相关:

// 给浏览器加cookie
    public void addCookie(Cookie cookie) {
      cookie.validate();
      execute(DriverCommand.ADD_COOKIE, ImmutableMap.of("cookie", cookie));
    }

// 删除浏览器的cookie(ByName)
    public void deleteCookieNamed(String name) {
      execute(DriverCommand.DELETE_COOKIE, ImmutableMap.of("name", name));
    }
// 删除浏览器的cookie
    public void deleteCookie(Cookie cookie) {
      deleteCookieNamed(cookie.getName());
    }

// 删除浏览器的所有cookie
    public void deleteAllCookies() {
      execute(DriverCommand.DELETE_ALL_COOKIES);
    }
// 获取浏览器的cookie
    @SuppressWarnings({"unchecked"})
    public Set<Cookie> getCookies() {}

// 获取浏览器的cookie byName
    public Cookie getCookieNamed(String name) {

Driver本身相关:

// 浏览器将打开的链接地址
public void get(String url) {}

// 获取浏览器页面的标题
public String getTitle() {}

// 获取浏览器当前页面的URL
public String getCurrentUrl() {}

// 当前浏览器截图并且输出为某种文件格式
public <X> X getScreenshotAs(OutputType<X> outputType) throws WebDriverException {}

// 获取当前页面的html树
public String getPageSource() {
    return (String) execute(DriverCommand.GET_PAGE_SOURCE).getValue();
  }

// 关闭当前的windows
  public void close() {
    execute(DriverCommand.CLOSE);
  }

// 关闭当前的session
  public void quit() {
    // no-op if session id is null. We're only going to make ourselves unhappy
    if (sessionId == null) {
      return;
    }

    try {
      execute(DriverCommand.QUIT);
    } finally {
      sessionId = null;
      stopClient();
    }
  }

// 获取当前Windows的数量
  public String getWindowHandle() {
    return String.valueOf(execute(DriverCommand.GET_CURRENT_WINDOW_HANDLE).getValue());
  }

寻找元素:

// 找符合定位策略的所有元素 
  public List<WebElement> findElements(By by) {
    return by.findElements(this);
  }

// 找符合定位策略的第一个元素 

  public WebElement findElement(By by) {
    return by.findElement(this);
  }

// 以下方法都是节省传入By.id / By.TabName等的语法糖,可以直接传入定位的string而不用再传入By.id等。
  public WebElement findElementById(String using) {
    return findElement("id", using);
  }

  public List<WebElement> findElementsById(String using) {
    return findElements("id", using);
  }

  public WebElement findElementByLinkText(String using) {
    return findElement("link text", using);
  }

  public List<WebElement> findElementsByLinkText(String using) {
    return findElements("link text", using);
  }

  public WebElement findElementByPartialLinkText(String using) {
    return findElement("partial link text", using);
  }

  public List<WebElement> findElementsByPartialLinkText(String using) {
    return findElements("partial link text", using);
  }

  public WebElement findElementByTagName(String using) {
    return findElement("tag name", using);
  }

  public List<WebElement> findElementsByTagName(String using) {
    return findElements("tag name", using);
  }

  public WebElement findElementByName(String using) {
    return findElement("name", using);
  }

  public List<WebElement> findElementsByName(String using) {
    return findElements("name", using);
  }

  public WebElement findElementByClassName(String using) {
    return findElement("class name", using);
  }

  public List<WebElement> findElementsByClassName(String using) {
    return findElements("class name", using);
  }

  public WebElement findElementByCssSelector(String using) {
    return findElement("css selector", using);
  }

  public List<WebElement> findElementsByCssSelector(String using) {
    return findElements("css selector", using);
  }

  public WebElement findElementByXPath(String using) {
    return findElement("xpath", using);
  }

  public List<WebElement> findElementsByXPath(String using) {
    return findElements("xpath", using);
  }

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值