Selenium驱动IE的内部体系
The diagram above illustrates the design of the InternetExplorerDriver at a high level. The general flow of control is from left to right, so the obvious place to start when working on the InternetExplorerDriver is on the far left, where the test code calls into the driver!
Key files: webdriver.h
Language bindings are (or should be) thin shims around the underlying driver. In the case of the InternetExplorerDriver, we communicate from the "Object Based" language bindings to a flattened API presented in webdriver.h. These are presented as a series of exported C functions, and so can be called without difficulty using libraries such as JNA, ctypes, pinvoke or DL. This increases the complexity of implementing the driver itself, but makes writing new clients a lot easier. It also means that clients are responsible for memory management.
The rule of thumb: if you ask the driver for it, you're responsible for ensuring that the memory is cleared. The basic pattern is that followed by theStringWrapper, who's usage is demonstrated below (in C, and omitting any checking of error codes):
StringWrapper* wrapper;
wdGetCurrentUrl(driver, &wrapper);
/* Use the wrapper */
// Now free it
errorCode = wdFreeString(wrapper);
In languages that support object lifecycles, you may find it easiest to ensure that the handle is freed in an object's finalizer or destructor.
Key files: errorcodes.h
Each function exposed from webdriver.h returns an int as a result. When the function succeeds, this result will be 0. In all other cases, an error code is returned, as indicated in errorcodes.h. Client implementations are free to interpret these response codes as they wish to, but the Java code attempts to give meaningful exceptions.
Key files: IEThread.h, InternetExplorerDriver.cpp, IEThreadExplorer.cpp
The C code in webdriver.h is ultimately a thin wrapper around C++ classes that model the the Object-based design of the Java code (theInternetExplorerDriver (C++) and IEThreadExplorer (C++) in the diagram) The underlying IE COM interfaces are designed for use in a Single Thread Apartment (STA) model. That is, the COM object must only ever be accessed from a single thread. Unfortunately, we cannot control how many threads call into our implementing library, and there are perfectly reasonable occasions where more than one thread may attempt to call the underlying library, even if in serial fashion (for example, the RemoteWebDriver is hosted in a servlet container, where many threads may be in use) In order to isolate the COM interfaces, we pass messages across a thread boundary using the Win32 PostMessage API.
This design causes a certain amount of complexity to enter the InternetExplorerDriver, but does mean that client code can afford to be ignorant of the constraints offered by the underlying implementation: this is seen as being a Good Thing by the webdriver team.
Key files: interactions.cpp
There are two ways that we could simulate keyboard and mouse input. The first way, which is used in parts of webdriver, is to synthesize events on the DOM. This has a number of drawbacks, since each browser (and version of a browser) has its own unique quirks; to model each of these is a demanding task, and impossible to get completely right (for example, it's hard to tell what window.selection should be and this is a read-only property on some browsers) The alternative approach is to synthesize keyboard and mouse input at the OS level, ideally without stealing focus from the user (who tends to be doing other things on their computer as long-running webdriver tests run) The code for doing this is in interactions.cpp The key thing to note here is that we use PostMessages to push window events on to the message queue of the IE instance. Typing, in particular, is interesting: we only send the "keydown" and "keyup" messages. The "keypress" event is created if necessary by IE's internal event processing. Because the key press event is not always generated (for example, not every character is printable, and if the default event bubbling is cancelled, listeners don't see the key press event) we send a "probe" event in after the key down. Once we see that this has been processed, we know that the key press event is on the stack of events to be processed, and that it is safe to send the key up event. If this was not done, it is possible for events to fire in the wrong order, which is definitely sub-optimal.
Currently, all the tests for the InternetExplorerDriver are written in Java, and so you'll need both Visual Studio 2005 and a Java IDE installed on your machine. If you're using Eclipse, the process for making and testing modifications is:
- Edit the C++ code in VS.
- Build the code to ensure that it compiles
- Do a complete rebuild when you are ready to run a test. This will cause the created DLL to be copied to the right place to allow its use in Eclipse
- Load Eclipse (or some other IDE, such as Idea)
- Edit the SingleTestSuite so that it is usingDriver(IE)
- Create a JUnit run configuration that uses the "webdriver-internet-explorer" project. If you don't do this, the test won't work at all, and there will be a somewhat cryptic error message on the console.
Once the basic setup is done, you can start working on the code pretty quickly. As an alternative, there will soon be .Net bindings added (using C#) Once these have been added, it should be possible to make changes to the IE driver entirely in Visual Studio.
When working with Java, any unhandled exception in the C++ code may cause the JVM to die. If this happens, take a look at the log files that Java generates. This will give you a hint at where to start when tracking down the problem. When the IE driver is compiled in "Release" mode, we turn on Structured Exception Handling, which means that rather than taking out the JVM an IllegalStateException is thrown.
分享到:
相关推荐
Selenium 是一个强大的开源自动化测试框架,主要用于Web应用程序的测试。它允许开发人员编写可运行在多种浏览器和操作系统上的测试脚本,极大地提高了测试的覆盖率和效率。Selenium 支持多种编程语言,如Java、...
写了个简单的例子,供大家参考。 实现了以下功能: 1、打开浏览器,进入百度主页 2、打开登录窗口 3、切换到登录窗口 4、输入账号跟密码,并点击登录 5、切换回原来的窗口,打印当前城市的天气 6、关闭浏览器
selenium+firefox在定位时遇到selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: 由于是js加载页面,想确认是否是js的原因,随后进行多次调试时发现“//div”竟然也出现了...
selenium爬虫学习——CNKI内容数据获取
本文介绍的是一本关于Selenium自动化测试的实践指南,它以Python语言为编程基础,提供了一系列自动化测试的实践技巧和实例。以下知识点将详细介绍书中所涉及的关键内容。 首先,了解Selenium。Selenium是一个用于...
《Selenium2自动化测试实战——基于Python语言》是一本针对软件测试工程师,尤其是对Web应用程序自动化测试感兴趣的读者的专业书籍。作者虫师在2016年10月编写了这本书,尽管时间已过去数年,但Selenium作为自动化...
from selenium import webdriver 2.浏览器获取驱动 需要下载跟chrome浏览器相匹配的驱动driverchrome.exe,详情见:根据电脑浏览器的版本下载相应的驱动chromedriver.exe,环境变量的配置,详情见这里Window 下配置...
Selenium 是一个强大的Web自动化测试工具,但同时也被广泛用于网络爬虫开发,特别是在处理动态网页时。它允许程序员模拟真实用户在浏览器中的操作,包括点击、滚动、填写表单等,从而获取所需的数据。在传统的爬虫...
在“RFS——RobotFramework+Selenium2library安装包齐全”中,我们有完整的环境来搭建和运行基于Robot Framework和Selenium2Library的自动化测试项目。 首先,让我们深入理解Robot Framework。Robot Framework是一...
Selenium 是一个强大的开源自动化测试框架,用于网页应用。它支持多种编程语言,如 Java、Python、C#、Ruby 等,使测试工程师能够编写脚本来模拟用户在浏览器中的各种交互行为。Selenium 4.5.0 版本是该框架的一个...
selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium
Selenium 也是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE、Mozilla Firefox、Google Chrome等。这个工具的主要功能包括:测试与浏览器的兼容性...
标题 "selenium-java-4.0.0-alpha-6_javaselenium_" 指的是 Selenium 的一个 Java 版本的软件包,具体是 4.0.0 的第六个 Alpha 版本。Selenium 是一个广泛使用的自动化测试工具,主要用于 web 应用程序的测试。它...
在IT行业中,Selenium是一个广泛使用的自动化测试框架,主要用于Web应用程序的测试。它提供了一组强大的API,使得开发者能够用多种编程语言(如Java、Python、C#等)编写自动化测试脚本。在这个主题中,我们将深入...
### Selenium 知识点详解 #### 一、Selenium 概述 Selenium 是一个用于自动化 Web 应用程序测试的工具集。它由 ThoughtWorks 公司开发,并且随着时间的发展已经成为一个开源项目,得到了广泛的社区支持。Selenium ...
【Selenium 2 自动化测试实战 基于PYTHON语言】 Selenium 是一款强大的Web自动化测试工具,尤其适用于浏览器自动化。Selenium 2(也称为Selenium WebDriver)是其发展的一个重要阶段,它提供了对多种浏览器的支持,...