WebDriver API——浏览器控制暨如何学习webdriver API
在测试过程中我们可能需要对浏览器进行控制,大小控制啊,刷新页面啊,前进后退等等,最常用的两个接口是window和Navigation。
我们最常用的就是这4个,那么你是否感兴趣它们后面是什么,它们是怎么写出来的,我能否通过这几个简单的方法拓展自己思路,而不是等用到了去百度,一起来看下

1.window接口

返回了一些方法来管理当前窗口,那么有哪些呢?我们来看下
interface Window {
/**
* Set the size of the current window. This will change the outer window dimension,
* not just the view port, synonymous to window.resizeTo() in JS.
*
* @param targetSize The target size.
*/
void setSize(Dimension targetSize);
/**
* Set the position of the current window. This is relative to the upper left corner of the
* screen, synonymous to window.moveTo() in JS.
*
* @param targetPosition The target position of the window.
*/
void setPosition(Point targetPosition);
/**
* Get the size of the current window. This will return the outer window dimension, not just
* the view port.
*
* @return The current window size.
*/
Dimension getSize();
/**
* Get the position of the current window, relative to the upper left corner of the screen.
*
* @return The current window position.
*/
Point getPosition();
/**
* Maximizes the current window if it is not already maximized
*/
void maximize();
}
可以看到我们可以对当前窗口进行大小调整,进行位置调整,获取浏览器窗口大小,最大化等等。
2.Navigation接口

可以让driver来访问历史或者访问地址,这个接口可以做什么呢?
interface Navigation {
/**
* Move back a single "item" in the browser's history.
*/
void back();
/**
* Move a single "item" forward in the browser's history. Does nothing if we are on the latest
* page viewed.
*/
void forward();
/**
* Load a new web page in the current browser window. This is done using an HTTP GET operation,
* and the method will block until the load is complete. This will follow redirects issued
* either by the server or as a meta-redirect from within the returned HTML. Should a
* meta-redirect "rest" for any duration of time, it is best to wait until this timeout is over,
* since should the underlying page change whilst your test is executing the results of future
* calls against this interface will be against the freshly loaded page.
*
* @param url The URL to load. It is best to use a fully qualified URL
*/
void to(String url);
/**
* Overloaded version of {@link #to(String)} that makes it easy to pass in a URL.
*
* @param url
*/
void to(URL url);
/**
* Refresh the current page
*/
void refresh();
}
浏览器的后退,前进,访问url,刷新等操作。
那么我们还看到driver有个manage接口,顾名思义是管理的意思,我们可以看下它到底起到了哪些作用。点进去看下。

有道一下,用来管理你想操作的浏览器菜单,那么我们可以想下浏览器菜单里有什么,历史记录,书签等等等。来看下提供了哪些方法。
/**
* An interface for managing stuff you would do in a browser menu
*/
interface Options { /**
* Add a specific cookie. If the cookie's domain name is left blank, it is assumed that the
* cookie is meant for the domain of the current document.
*
* @param cookie The cookie to add.
*/
void addCookie(Cookie cookie); /**
* Delete the named cookie from the current domain. This is equivalent to setting the named
* cookie's expiry date to some time in the past.
*
* @param name The name of the cookie to delete
*/
void deleteCookieNamed(String name); /**
* Delete a cookie from the browser's "cookie jar". The domain of the cookie will be ignored.
*
* @param cookie
*/
void deleteCookie(Cookie cookie); /**
* Delete all the cookies for the current domain.
*/
void deleteAllCookies(); /**
* Get all the cookies for the current domain. This is the equivalent of calling
* "document.cookie" and parsing the result
*
* @return A Set of cookies for the current domain.
*/
Set<Cookie> getCookies(); /**
* Get a cookie with a given name.
*
* @param name the name of the cookie
* @return the cookie, or null if no cookie with the given name is present
*/
Cookie getCookieNamed(String name); /**
* Returns the interface for managing driver timeouts.
*/
Timeouts timeouts(); /**
* Returns the interface for controlling IME engines to generate complex-script input.
*/
ImeHandler ime(); /**
* Returns the interface for managing the current window.
*/
@Beta
Window window(); /**
* Gets the {@link Logs} interface used to fetch different types of logs.
*
* <p>To set the logging preferences {@link LoggingPreferences}.
*
* @return A Logs interface.
*/
@Beta
Logs logs();
}
这里面包含了对窗口的操作,对cookie的操作,对页面超时的处理,具体的可以再往里深究,在这里就不一一详述了,为大家提供一个思路。
-----------------------------------------------总结一下------------------------------------------------------
希望看到这篇文章的同学能有所启发,做过开发的同学可能觉得这很简单就是提供了许多接口给咱们调用,那么没有做过开发的同学肯定不会有这样的一个开发思路,点开每个方法,每个参数,每个接口去看下它后台是怎么实现的。那么我希望这部分同学在学习webdriver的时候能多些好奇心,拓展下自己的思维,而不是遇到了一个陌生的问题就去度娘,你比如我想操作浏览器前进,就去百度了怎么控制浏览器前进,那么等你想操作cookie的时候你还是会去百度,如果平时没事点开看下源码,那对于提高学习效率和开拓思维是极好的,,对提高自己的测试开发能力也是极好的。
WebDriver API——浏览器控制暨如何学习webdriver API的更多相关文章
- Python3 Selenium自动化web测试 ==> 第三节 常用WebDriver API使用示例上(24个API)
前置步骤: 安装selenium,chrome驱动,Python3.6 学习目的: 常见API的使用 涉及的API: step1: 访问一个网址 step2: 网页的前进和后退 step3: 刷新当前 ...
- 使用python selenium webdriver模拟浏览器
selenium是进行web自动化测试的一个工具,支持C,C++,Python,Java等语言,他能够实现模拟手工操作浏览器,进行自动化,通过webdriver驱动浏览器操作,我使用的是chrome浏 ...
- 浅谈python中selenium库调动webdriver驱动浏览器的实现原理
最近学web自动化时用到selenium库,感觉很神奇,遂琢磨了一下,写了点心得. 当我们输入以下三行代码并执行时,会发现新打开了一个浏览器窗口并访问了百度首页,然而这是怎么做到的呢? from se ...
- [技术博客]基于动态继承类、WebDriver的浏览器兼容性测试框架搭建
问题背景 观察使用selenium进行自动化测试的过程,我们可以将它概述为: 启动测试进程,在该进程中构建WebDriver 启动浏览器进程,将它与WebDriver建立连接 使用WebDriver向 ...
- selenium webdriver(1)---浏览器操作
启动浏览器 如何启动浏览器已在上篇文章中说明,这里还是以chrome为例,firefox.IE启动方式相同. //启动浏览器 import org.openqa.selenium.WebDriver; ...
- Java+selenium之WebDriver对浏览器的简单操作(一)
操作浏览器的主要方法都来自 org.openqa.selenium.WebDriver 这个接口 这些方法都是在 org.openqa.selenium.remote.RemoteWebDriver这 ...
- selenium WebDriver 对浏览器标签页的切换
关于selenium WebDriver 对浏览器标签页的切换,现在的市面上最新的浏览器,当点击一个链接打开一个新的页面都是在浏览器中打开一个标签页,而selenium只能对窗口进行切换的方法,只能操 ...
- 查看webdriver针对浏览器的一些函数
在用webdriver对浏览器进行操作时,很多操作并不是那么好找,后来在朋友的推荐下可以用下面的方法来寻找针对浏览器的一些操作,函数或属性等,这样方便我们可以查找一些方法去完成我们要的操作. 下面是查 ...
- WebDriver 将浏览器窗口最大化
package com.entrym.main; import java.io.File; import java.io.IOException; import org.openqa.selenium ...
随机推荐
- MySQL中数据类型(char(n)、varchar(n)、nchar(n)、nvarchar(n)的区别)(转)
一.第一种 char(n)和varchar(n)的区别: 在这里我们可以清楚的看到他们表面的区别就是前面是否有var,在这里解释一下var是什么意思,var代表“可变的”的意思 下面看个例子: )// ...
- IDEA一个窗口打开多个项目
首先IDEA没有Eclipse的Workspace的概念,且IDEA推荐是一个窗口对应着一个Project. 然后经过研究你会发现IDEA其实是由一个主进程来维护这些窗口的,所以即使你开了很多个窗口, ...
- webstorm 2016 激活破解
2017.2.27更新 选择“license server” 输入:http://idea.imsxm.com/ 2016.2.2 版本的破解方式: 安装以后,打开软件会弹出一个对话框:选择“lice ...
- Mac下export生效
在Terminal下用export PS1=XXX 修改完后,本次生效,但是重新启动Teminal后又恢复到默认格式.如何才能永久保存自定义的提示符格式呢? 1,~下面本来没有 .bash_pro ...
- LattePanda 项目之 P2.2 起飞条件检测系统(CLI & GUI)
前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正常的链接.图片显示,请访问我的博客原文: http://www.cnblog ...
- ios 清理缓存(EGO)
一段清理缓存的代码例如以下: dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,) , ^{ NSSt ...
- odoo税金处理
税金可以设置为'税金包含在价格中',或者'税金不包含在价格中'. 在税金计算处理过程中,odoo会将价格/金额按 total_included/ total_exincluded 分开 ...
- 【每日Scrum】第七天(4.28)Sprint2总结性会议
本次会议主要是演示了一下本组项目的各项功能,每个人负责那一块儿功能由本人来负责说明和演示,确定alpha版本的发布时间,并且分派了各组员的文档负责情况,上图是会议记录,下面我详细介绍一下我组分派情况: ...
- vim 模式切换
1. 从插入模式退回到normal模式 <esc> <C-c> <C-[>
- IOS 汤姆猫核心代码
// // MJViewController.m // 03-Tom // // Created by apple on 13-11-24. // Copyright (c) 2013年 itcast ...