Selenium Webdriver re-use an open browser instance

Source: Internet
Author: User
Tags soapui owasp zap groovy script

The samples in this article all use SOAPUI, about the configuration of soapui+webdriver, please see an article:
http://blog.csdn.net/wwwqjpcom/article/details/51174664

I did this in order to better write the automation use case in SOAPUI because my business process has a long, 7-8 page.
I want to keep the code out of a groovy script and want to continue using the browser opened in the first script in the second script. This facilitates
Maintain and locate problems.
There is also a situation is that I opened the browser, operating the system to a certain interface, I wrote this page test script, using the
Open Browser I can immediately test this page alone to test whether the code I wrote is OK. Do not reset the page by manual operation,
After modifying the code, test again, not every time the test code is feasible to punch the browser, log into the system, re-operation. can achieve step
Single-page debugging Automation scripts.

First, let's take a quick look at how selenium webdriver works.
(1) The Selenium code call API actually sends different HTTP Request to Webdriverserver according to the webdriver wire Protocol.
IE is IEDriverServer.exe
Chrome is Chromerdriver,: https://sites.google.com/a/chromium.org/chromedriver/downloads
Firefox is in the form of plugins, directly in the Selenium-server-standalone-xxx.jar:
Webdriver.xpi (Selenium-server-standalone-2.48.2.jar in/org/openqa/selenium/firefox/directory)

New Firefoxdriver (), when you start the Firefox browser, with this plug-in started together, and then the plug-in will default to listen to 7055 port, 7055 is occupied by using the next port. As shown in.

Multiple Firefoxdriver instances can be started on the same machine at the same time, with each instance occupying a different port number.

The WebDriver Wire Protocol 协议的具体内容请看:https://code.google.com/p/selenium/wiki/JsonWireProtocol#Introduction。  这个协议现在正在被W3C标准化,W3C Webdriver,两者基本一样。  W3C Webdriver标准协议内容:http://www.w3.org/TR/webdriver/

(2) After receiving the HTTP request, Webdriverserver triggers the browser's "native event" at the operating system level according to different commands.
Simulates the operation of the browser. Webdriverserver returns the operation result HTTP response to the client for the code call.

To see how this works more clearly and intuitively, let's take a look at using owasp Zap as an agent, intercepting HTTP request and response.
Install owasp Zap or other agent-enabled tools first, set up SOAPUI proxy, if Zap uses port 8080 by default, the SOAPUI configuration is as follows:

After configuring the SOAPUI port, it appears as if you need to restart Soapui, and then in SOAPUI's automated test code, the agent works correctly.

Rerun the previous section of the Firefoxdriver code to view intercepted requests and responses. As shown in the following:


You can see clearly how the code is connected to the Driverserver based on the Webdriver protocol.
The Webdriver protocol is restful in style.

back to our topic: Webdriver re-use an open browser instance.
The Webdriver instance will reopen a browser and create a new session. Selenium does not have an interface or method to use an existing session.
What if you don't close the browser in one test case and want to use an open browser in another new test case?
Selenium Webdriver does not have this interface in the province. I realized a firefoxdriver based on my own, because I basically only use Firefox,
IE something, no need, no power ah, but probably the principle should be similar, almost.

Firefoxdriver is primarily initialized by Startclient () and Startsession (). Startclient () Opens the browser and sets the actuator httpcommandexecutor.
Startsession executes the new session instruction, gets the SessionID, and sets the capabilities based on the response.
Specific interested can download their own source code, see for themselves.

According to the above, Webdriver wants to execute the command, requiring 1:webdriverserver address 2: An available SessionID .
Write one of your own Webdriver classes to new a webdriver.

Therefore, you need to save the Webdriverserver address and SessionID in one use case, and finally close the browser without exiting.
Another use case uses the saved parameters to complete the instantiation of the Webdriver.
The code is not posted in the article: source code and JAR package:
http://download.csdn.net/detail/wwwqjpcom/9500777

The initialization code is as follows (requires two parameter Webdriver address and SessionID):

    publicmyFirefoxDriver(String localserver,String sessionID){        mystartClient(localserver);        mystartSession(sessionID);    }

code example:
Test Case 1 opens the browser, but does not exit the browser by closing:

import org. Openqa. Selenium. byimport org. Openqa. Selenium. Webdriverimport org. Openqa. Selenium. Webelementimport org. Openqa. Selenium. Firefox. Firefoxdriverimport org. Openqa. Selenium. Support. UI. Expectedconditionimport org. Openqa. Selenium. Support. UI. Webdriverwaitimport org. Openqa. Selenium. OutputTypeimport org. Apache. Commons. IO. FileUtilsimport org. Openqa. Selenium. KeysWebdriver Driver = new Firefoxdriver () try{driver. Get("Https://learnsoapui.wordpress.com")//URL to is opened//the desired address and SessionID are saved for the following two rows. The sample is saved as a soapui of two step in the SOAPUI, so save it as a system parameter or somewhere else, depending on your usage environment Testrunner. TestCase. setPropertyValue("Driverserver", Driver. Getcommandexecutor(). Getaddressofremoteserver(). toString()) Testrunner. TestCase. setPropertyValue("Casesession", Driver. GetSessionID(). toString()) Log. InfoDriver. GetSessionID(). toString() webelement element = Driver. Findelement(By. ID("S")) Element. SendKeys("Assertion") File F1 = Driver. Getscreenshotas(OutputType. FILE) FileUtils. CopyFile(F1, New File ("C:\\screenshot1.png"));//location to save screenshotElement. Submit() driver. Getkeyboard(). Presskey(Keys. down) Driver. Getkeyboard(). Presskey(Keys. down) Driver. Getkeyboard(). Presskey(Keys. down) Driver. Getkeyboard(). Presskey(Keys. up) Driver. Getkeyboard(). Presskey(Keys. up) Driver. Getkeyboard(). Presskey(Keys. up)}catch (Exception e) {log. Info "Exception encountered:"+ E. Message}

The open browser in test Case 2 continues to be used in 1:

Import webtest. Myfirefoxdriver;import org. Openqa. Selenium. byimport org. Openqa. Selenium. Webdriverimport org. Openqa. Selenium. Webelementimport org. Openqa. Selenium. Firefox. Firefoxdriverimport org. Openqa. Selenium. JavascriptexecutorOn the following three lines, remove the address of the Webdriver server that saved the available browser and sessionid,new a webdriver. def driverserver = Testrunner. TestCase. GetPropertyValue("Driverserver") def casesession = Testrunner. TestCase. GetPropertyValue("Casesession") Webdriver Driver = new Myfirefoxdriver (driverserver,casesession) log. Info(Driver. Getcommandexecutor(). Getaddressofremoteserver()) Try{driver. Findelement(By. LinkText("Home")). Click()//URL to IS Openeddriver. Findelement(By. LinkText("About Author")). Click()//URL to is opened log. InfoDriver. GetSessionID(). toString() Log. InfoDriver. Getcapabilities() ((javascriptexecutor) driver). Executescript("alert (\" Hello,this is a alert!\ ")");Driver. Quit()}catch (Exception e) {log. Info "Exception encountered:"+ E. Message}


Note the text in the code below the section, 1 to save Webdriverserver address and SessionID,
2 uses the parameters saved in 1 and initializes the driver with the implementation's own myfirefoxdriver.
(Thanks to https://learnsoapui.wordpress.com/, I originally studied the use of selenium in Soapui
Webdriver saw this, although he said the feeling is unclear, but gave me inspiration.

注:(1)webtest01.jar是我在Selenium 2.48.2版本的代码下编译的,本人只配合用过Selenium 2.48.2          和Selenium 2.53.0这两个版本可用,其他的Selenium版本我是没试过的,说不定老版本不支持的。)   (2)实际中myFirefoxDriver不仅可以在本地用,用来RemoteWebdriver远程调用也是可以用的,即打开         RemoteWebdriver,然后用myFirefoxDriver来继续使用远程的那个已打开的浏览器实例,反正是只需要那两         个参数就可以,反正我自己用的时候这种情况也是是可以用的。仅供参考,不提供技术支持,呃,后果自负哦。

Selenium Webdriver re-use an open browser instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.