Software testing tools

Selenium software testing tools

Selenium is a tool for Web application testing is one of the most traditional and most popular testing tools. Selenium tests run directly in the browser, just as real users in the same operation. Supported browsers include IE (7, 8, 9, 10, 11), Mozilla Firefox, Safari, Google Chrome, Opera and so on. The main features of this tool include: testing and browser compatibility - Test your application to see if the well had to work on different browsers and operating systems. Test system functions - create regression testing test software functionality and user requirements. Support automatically record test scripts and actions in different languages ​​automatically generate .Net, Java, Perl and so on. Many businesses prefer Selenium, because the tool is compatible with a variety of programming languages, and can be integrated with the testing framework. Its advantage is that it is open source, can be easily integrated with other tools and frameworks.

Installation Selenium 

Because Selenium need to control your browser, so to install than conventional Python module to more than a few steps. First use pip install selenium in the terminal or cmd.

 

 

To control the browser , you must have a driver browser. Selenium for several mainstream browsers have a driver.  For Linux and MacOS .

After a good Linux and MacOS users to download, please download good "geckodriver" file on your computer "/ usr / bin" or "/ usr / local / bin" directory and given execute permission, will not put, use this statement.

 

 

For Windows users, the official line of argument , does not seem to mention specifically how to operate, I think, should be the location of the file geckodriver added to the Windows environment variables (PATH).

If you have installed any questions , please on their official website queries on solutions .

 


First, the statement browser object note a point, Python file name or package name not named as selenium, can cause can not be imported from the Selenium Import webdriver #webdriver can be considered to drive the browser, the browser must be used to drive webdriver, support multi kind of browser, Chrome here for an example browser = webdriver.Chrome () Second, the access page and get the page HTML from the Selenium Import webdriver browser = webdriver.Chrome () browser.get ( 'https://www.taobao.com' ) Print (browser.page_source) # browser.page_source page is to get all the HTML browser.close () Third, find elements of a single element from the Selenium webdriver Import Browser = webdriver.Chrome () browser.get ( 'HTTPS: // the WWW. taobao.com ') input_first = browser.find_element_by_id (' Q ') input_second = browser.find_element_by_css_selector (' # Q ') = browser.find_element_by_xpath input_third ( '// * [@ ID = "Q"]') Print (input_first, input_second, input_third) browser.close () common to find a method find_element_by_name find_element_by_xpath find_element_by_link_text find_element_by_partial_link_text find_element_by_tag_name find_element_by_class_name find_element_by_css_selector may be a general-purpose method Import the webdriver Selenium from from selenium.webdriver.common.by Import By Browser = webdriver.Chrome () browser.get ( 'https://www.taobao.com') input_first = browser.find_element (BY.ID, 'Q' ) # the first argument name, specific parameters passed in the second Print (input_first) browser.close () a plurality of elements, a plurality of elements s input_first = browser.find_elements_by_id ( 'Q') four elements interoperate - incoming keyword search box automatically search Selenium the webdriver Import from Import Time Browser = webdriver.Chrome () browser.get ( 'https://www.taobao.com') INPUT = browser.find_element_by_id ( 'Q') # find search box input.send_keys ( 'iPhone' ) # Image transfer into the time.sleep (. 5) input.clear () # clear the search box input.send_keys ( 'men's underwear') button = browser.find_element_by_class_name ( 'BTN-Search') to find the search button # Button.Click ( ) more: You can have attributes, screenshots etc. http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement# five interactive action, drive browser action, simulation drag action, the operation to attach a serial chain operation performed from the webdriver Selenium Import from selenium.webdriver ActionChains # Import operation chain introduced webdriver import ActionChains # chain introducing operation browser = webdriver.Chrome () = URL 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable' browser.get (URL) browser.switch_to.frame ( 'iframeResult') is switched to iframeResult frame # source = browser.find_element_by_css_selector ( '# draggable') # find the object being dragged target = browser.find_element_by_css_selector ( '# droppable' ) # find the target actions = ActionChains (browser) # declared actions target actions.drag_and_drop (Source, target) actions.perform () # perform actions more actions: http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains six execute JavaScript some actions may not be available api, such as the progress bar drop-down, In this case, we can code execution by the JavaScript from the webdriver Selenium Import Browser = webdriver.Chrome () browser.get ( 'https://www.zhihu.com/explore') browser.execute_script (' the window.scrollTo (0, document.body.scrollHeight) ') browser.execute_script ( 'alert ( "To Bottom ")') VII element information acquiring acquires the attribute from the webdriver Selenium Import from selenium.webdriver Import ActionChains Browser = webdriver.Chrome () URL = 'https://www.zhihu.com / Explore ' browser.get (URL) logo browser.find_element_by_id = (' Top-ZH-logo-Link ') access to the Web # logo Print (logo) Print (logo.get_attribute (' class')) browser.close () Gets text value from the webdriver Selenium Import Browser = webdriver.Chrome () URL = 'https://www.zhihu.com/explore' browser.get (URL) input = browser.find_element_by_class_name('zu-top-add-question') Print (input.text) # input.text text value browser.close ( ) # get Id, location, tag name, size from selenium import webdriver = webdriver.Chrome Browser () URL = 'https://www.zhihu.com/explore' browser.get (URL) INPUT = browser.find_element_by_class_name ( 'the Add-zu-Top-Question') Print (input.id) # obtain the above mentioned id Print (input.location) # get position print (input.tag_name) # get the tag name print (input.size) # get the size of browser.close () eight, Frame operating frame corresponds to a separate page, if the parent network-based lookup subclass frame, the frame must switch to subclass, the subclass if need be to find a parent class switching from the webdriver Selenium Import from selenium.common.exceptions Import NoSuchElementException Browser = webdriver.Chrome () URL = 'HTTP : //www.runoob.com/try/try.php filename = jQueryUI-API-droppable '? browser.get (url) browser.switch_to.frame (' iframeResult ') = browser.find_element_by_css_selector Source ( 'draggable with #') Print (Source) the try: logo = browser.find_element_by_class_name ( 'logo') the except NoSuchElementException: Print ( 'NO LOGO') browser.switch_to.parent_frame () logo browser.find_element_by_class_name = ( 'logo') Print (logo) Print (logo.text) nine, waiting implicitly waiting when using the implicit test of time waiting to be executed, if WebDriver not find the elements in the DOM, will continue to wait, then after the set time throws an exception can not find the elements, in other words, when the element or elements to find did not immediately appear, implicitly wait will wait for some time to find the DOM, the default time is 0 from the Selenium webdriver Import Browser = webdriver.Chrome () browser.implicitly_wait (10) # wait ten seconds it will throw an exception is not loaded, loading it returns to normal within 10 seconds browser.get ( 'https://www.zhihu.com/explore') browser.find_element_by_class_name = INPUT ( 'the Add-zu-Top-Question') Print (INPUT) explicitly waiting specify a wait condition, and a maximum wait time, the program determines whether the conditions are satisfied within the waiting time, if satisfied return if not satisfied will continue to wait, over time it will throw an exception from the Selenium Import webdriver from selenium.webdriver.common.by By Import from selenium.webdriver.support.ui Import WebDriverWait from selenium.webdriver.support Import expected_conditions AS EC Browser webdriver.Chrome = () browser.get ( 'https://www.taobao.com/') the wait = WebDriverWait (Browser, 10) INPUT = wait.until (EC.presence_of_element_located ((By.ID, 'Q') )) Button = wait.until (EC.element_to_be_clickable ((By.CSS_SELECTOR, '.btn-Search'))) Print (INPUT, Button) title_is is a content title title_contains header contains a content presence_of_element_located the loading element, positioning the incoming tuple, such as (By.ID, 'p') visibility_of_element_located element is visible, the incoming positioning tuple visibility_of visible, passing element object presence_of_all_elements_located all the elements to load a text_to_be_present_in_element an element text contains a text text_to_be_present_in_element_value an element value contains a text frame_to_be_available_and_switch_to_it frame to load and switch invisibility_of_element_located element is not visible element_to_be_clickable clickable elements staleness_of determine whether an element is still DOM, can determine whether the page has refreshed element_to_be_selected optional elements, pass elements of the object element_located_to_be_selected elements to choose from, the incoming positioning tuple element_selection_state_to_be incoming objects and elements of the state, equal returns True, otherwise return False element_located_selection_state_to_be pass meditation bytes and state, equal returns True, otherwise return False alert_is_present whether Alert appears Details: http: //selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions XI, forward and back - to achieve forward and backward in a different browser browse the Web Import Time from the Selenium Import the webdriver Browser = webdriver.Chrome () browser.get ( 'https://www.baidu.com/') browser.get ( 'https://www.taobao.com/') browser.get ( 'HTTPS: / /www.python.org/ ') browser.back () the time.sleep (. 1) browser.forward () browser.close () twelve, Cookies from the webdriver Selenium Import Browser = webdriver.Chrome () browser.get (' https://www.zhihu.com/explore ') Print (browser.get_cookies ()) browser.add_cookie ({' name ':' name ',' Domain ':' www.zhihu.com ','Value', 'germey'}) Print (browser.get_cookies ()) browser.delete_all_cookies () print(browser.get_cookies()) 选项卡管理 增加浏览器窗口 import time from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.baidu.com') browser.execute_script('window.open()') print(browser.window_handles) browser.switch_to_window(browser.window_handles[1]) browser.get('https://www.taobao.com') time.sleep(1) browser.switch_to_window(browser.window_handles[0]) browser.get('http://www.fishc.com') 十三、异常处理 from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.baidu.com') browser.find_element_by_id('hello') from selenium import webdriver from selenium.common.exceptions import TimeoutException, NoSuchElementException browser = webdriver.Chrome() try: browser.get('https://www.baidu.com') except TimeoutException: print('Time Out') try: browser.find_element_by_id('hello') except NoSuchElementException: print('No Element') finally: browser.close()

Guess you like

Origin www.cnblogs.com/neddyface/p/10963034.html