Python+selenium for cross-browser compatibility testing

Source: Internet
Author: User
Tags selenium python

python

https://www.python.org/

Python is a scripting language that is easy to learn and easy to use, enabling you to quickly implement business logic and integrate systems efficiently.

-----Http://zh.wikipedia.org/zh-cn/Python:

Its syntax is simple, unlike most other programming languages using curly braces, which use indentation to define a block of statements.

Python is often used for web development. For example, with the Mod_wsgi module, Apache can run web programs written in Python. Gunicor written in the Python language

n as a Web server, you can also run a Web program written in the Python language. Python defines the WSGI standard application interface to coordinate the communication between the HTTP server and the Python-based Web program. Some web frameworks, such as Django, Pyramid, TurboGears, Tornado, Web2py, Zope, and flask, can make it easy for programmers to develop and manage complex web programs.

Download: https://www.python.org/downloads/release/python-279/

Selenium python bindings

Selenium Python bindings provides a simple set of APIs that call selenium Webdriver through these APIs to implement functional and acceptance test cases.

Can support calls to Firefox and IE and Chrome.

Website Introduction:

Http://selenium-python.readthedocs.org/installation.html#introduction

Windows installation

1. Download and install python3.4 Https://www.python.org/ftp/python/3.4.2/python-3.4.2.msi

2. Use Cmd.exe to execute the following command, install selenium python bindings

C:\Python34\Scripts\pip.exe Install Selenium

3. Download Iedriver and Chromedriver, and put it in the Python installation directory (C:\PYTHON34)

Https://github.com/fanqingsong/daydayup/blob/master/CSoftware/webdriver/IEDriverServer.exe

Https://github.com/fanqingsong/daydayup/blob/master/CSoftware/webdriver/chromedriver.exe

Firefox does not need to drive

Use the method to study the drive:

Https://github.com/dineshkummarc/operadriver

Https://github.com/mfazekas/safaridriver

4. You can run a test script for Python, for example:

C:\Python34\python.exe C:\my_selenium_script.py

Official Website Installation Introduction:

Http://selenium-python.readthedocs.org/installation.html#downloading-python-bindings-for-selenium

Test Scripts

The following script implementation, open the Baidu Web page, search for a keyword, followed by three browsers (Firefox IE Chrome).

1 #Import OS
2 Import Time3 fromSeleniumImportWebdriver4 fromSelenium.webdriver.common.keysImportKeys5 6 #Test case7 defTestbrowser (Driver):8Driver.get ("http://www.baidu.com")9DRIVER.FIND_ELEMENT_BY_ID ("kw"). Click ()TenDRIVER.FIND_ELEMENT_BY_ID ("kw"). Clear () OneDRIVER.FIND_ELEMENT_BY_ID ("kw"). Send_keys ("VX") ADRIVER.FIND_ELEMENT_BY_ID ("su"). Click () -Driver.implicitly_wait (30) -Time.sleep (3) the driver.close () - driver.quit () - returnNone - +Driverfirefox =Webdriver. Firefox () - Testbrowser (Driverfirefox) + ADriverie =Webdriver. Ie () at Testbrowser (Driverie) - -Driverchrome =Webdriver. Chrome () -Testbrowser (Driverchrome)

The test case testbrowser code can intercept the body part using the script exported by the Selenium IDE:

#-*-coding:utf-8-*- fromSeleniumImportWebdriver fromSelenium.webdriver.common.byImport by fromSelenium.webdriver.common.keysImportKeys fromSelenium.webdriver.support.uiImportSelect fromSelenium.common.exceptionsImportnosuchelementexception fromSelenium.common.exceptionsImportnoalertpresentexceptionImportUnitTest, time, reclassBaidupython (unittest. TestCase):defsetUp (self): Self.driver=Webdriver. Firefox () self.driver.implicitly_wait (30) Self.base_url="http://www.baidu.com/"self.verificationerrors=[] Self.accept_next_alert=TruedefTest_baidupython (self): driver = Self.driver Driver.get (self.base_url + "/") driver.find_element_by_id ("kw"). Click () driver.find _element_by_id ("kw"). Clear () driver.find_element_by_id ("kw"). Send_keys ("VX") driver.find_element_by_id ("su") . Click ()defis_element_present (self, how, what):Try: Self.driver.find_element (By=how, value=What )exceptNosuchelementexception, E:returnFalsereturnTruedefis_alert_present (self):Try: Self.driver.switch_to_alert ()exceptNoalertpresentexception, E:returnFalsereturnTruedefClose_alert_and_get_its_text (self):Try: Alert=Self.driver.switch_to_alert () Alert_text=Alert.textifself.accept_next_alert:alert.accept ()Else: Alert.dismiss ()returnAlert_textfinally: Self.accept_next_alert =TruedefTearDown (self): Self.driver.quit () self.assertequal ([], self.verificationerrors)if __name__=="__main__": Unittest.main ()

You can also write your own script, using selenium driver's provided API:

Http://selenium-python.readthedocs.org/locating-elements.html

Python Tutorials:

Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 0013747381369301852037f35874be2b85aa318aad57bda000

Python+selenium for cross-browser compatibility testing

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.