Today, I would like to share with you some basic information about Selenium automated testing. Please feel free to comment if there is something wrong with Selenium automated testing.

1. Installation environment

1. Python environment

After the installation is complete, enter Python on the Windows command-line interface (CLI) to check whether the installation is successful

2. Install SetupTools and PIP

Setuptools is a side project of PEAK (Python Enterprise Application Kit). It is an enhancement of Python’s Distutilsde to make it easier to create and distribute Python packages, especially if they depend on other packages.

PIP is a tool for installing and managing Python packages. It is easy to install python packages by using PIP, and saves the tedious process. PIP installation relies on SetupTools.

3. Download the Selenium package

pip install selenium

4. Download the browser driver

Firefox and Google have their own drivers

Download link: www.seleniumhq.org/do…

A simple example

Example name: test_PYTHon_org_search.py

import unittest

 Unittest is a built-in Python module based on JAVAJUnit. This module provides a framework for organizing test cases

from selenium import webdriver

The # Selenium. Webdriver module provides all webDriver implementations

from selenium.webdriver.common.keys import Keys

The # Keys class provides all keyboard keystrokes

class PythonOrgSearch(unittest.TestCase):

TestCase. Inheriting TestCase tells the unittest module that this class is a TestCase

def setUp(self):

self.driver = webdriver.Firefox()

The #SetUp method is part of the initialization and creates an instance of Firefox WebDriver

def test_search_in_python_org(self):

driver = self.driver

driver.get("http://www.python.org")

The #driver.get method will open the site based on the URL given in the method

self.assertIn("Python", driver.title)

Use assert assertions to determine whether to include "Python" in the page title.

elem = driver.find_element_by_name("q")

# find DOM node with name q

elem.send_keys("pycon")

elem.send_keys(Keys.RETURN)

Press the enter key

assert "No results found." not in driver.page_source

def tearDown(self):

self.driver.close()

The #tearDown method is executed after each test method. This method can be used to do some cleaning, such as closing the browser. You can also call quit instead of 'close'

# Difference: Quit will close the entire browser, while 'close' will close only one TAB

if __name__ == "__main__":

unittest.main()

# entry function
Copy the code

Run directly from the shell: python test_python_org_search.py

Explanation of common methods

1. Open a page

Driver. The get (” www.baidu.com “)

WebDriver will wait until the page is fully loaded (in fact, until the onload method is complete) and then return to continue executing your script. It’s worth noting that if your page uses a lot of Ajax loading, WebDriver may not know when the page is fully loaded.

2. Interact with the page

WebDriver provides a number of methods to help you find elements, for example

<input type="text" name="passwd" id="passwd-id" />
Copy the code

It can be found in the following ways:

element = driver.find_element_by_id("passwd-id")

element = driver.find_element_by_name("passwd")

element = driver.find_element_by_xpath("//input[@id='passwd-id']")
Copy the code

You can also look it up by the text of the link, which, it’s important to note, must match exactly. When you use XPATH, you must be careful that if more than one element is matched, only the first element is returned. If none is found above, NoSuchElementException will be thrown.

Perform operations such as:

Enter something in the text box :element.send_keys(“some text”)

Clear content: element.clear()

Select drop-down box: WebDriver’s support class has a class called Select

From the selenium. Webdriver. Support. The UI import the Select

Select. Select_by_index (index) based on option order

Select. Select_by_visible_text (“text”) according to the text

Select. Select_by_value (value) according to its value value

Deselect: select.deselect_all()

Submit option: element.submit()

3. Drag and drop

 element = driver.find_element_by_name("source")

target = driver.find_element_by_name("target")

from selenium.webdriver import ActionChains

action_chains = ActionChains(driver)

action_chains.drag_and_drop(element, target).perform()
Copy the code

4. A dialog box is displayed

Alert = driver. Switch_to_alert ()

Switch_to_alert () # Locate pop-up dialog

Text () # Get the dialog box text value

Accept () # is equivalent to clicking “Ok”

Dismiss () # is equivalent to clicking “cancel”

Send_keys () # Enter values. Alert and confirm do not have input dialog boxes, so they are not available here, so they can only be used in prompt.

If software testing, interface testing, automated testing, interview experience exchange. If you are interested, you can add software test exchange: 1085991341, and there will be technical exchanges with peers.

5. Operating cookie

Open a page driver.get(” www.example.com “)

Cookie = {‘ name ‘:’ foo ‘, ‘value’ : ‘bar’} driver.add_cookie(cookie)

Now get all Cookies driver.get_cookies() available under the current URL

6. Find elements

find_element_by_id

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

The above content hopes to be helpful to you, have been helped to the friends welcome to praise, comment.