Write selenium test code in python

Overview

I needed to write end-to-end test code in python, so Make a note of what you are doing.

testing environment

Use the docker environment described in another article below.

** Build E2E test environment (python3 + selenium) with docker ** http://qiita.com/reflet/items/89ff50c991168adb3a9b

Write a simple test code and run it

Create a code by referring to the simple test code on the following site

** [Reference site] 2.1. Simple Usage ** http://selenium-python.readthedocs.io/getting-started.html#simple-usage

python_org_search.py


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote(
    command_executor='http://selenium-hub:4444/wd/hub',
    desired_capabilities=DesiredCapabilities.CHROME)

driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

Try running the above code

Terminal


# python python_org_search.py

Write a unit test

Create a test code referring to the following site and try to execute it.

** [Reference site] 2.3. Using Selenium to write tests ** http://selenium-python.readthedocs.io/getting-started.html#using-selenium-to-write-tests

test_python_org_search.py


import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class PythonOrgSearch(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Remote(
            command_executor='http://selenium-hub:4444/wd/hub',
            desired_capabilities=DesiredCapabilities.CHROME)

    def test_search_in_python_org(self):
        driver = self.driver
        driver.get("http://www.python.org")
        self.assertIn("Python", driver.title)
        elem = driver.find_element_by_name("q")
        elem.send_keys("pycon")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

Terminal


# python test_python_org_search.py

Reference site

Recommended Posts

Write selenium test code in python
Scraping with selenium in Python
Write standard input in code
Write beta distribution in Python
Algorithm in Python (primality test)
Write python in Rstudio (reticulate)
Write Spigot in VS Code
Scraping with Selenium in Python
Generate QR code in Python
Character code learned in Python
Set python test in jenkins
I want to write in Python! (1) Code format check
Write code to Unit Test a Python web app
Write a binary search in Python
Write a table-driven test in C
[Python] Generate QR code in memory
Write JSON Schema in Python DSL
Scraping with Selenium in Python (Basic)
Automatically format Python code in Vim
Write an HTTP / 2 server in Python
Write AWS Lambda function in Python
Write A * (A-star) algorithm in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Statistical test (multiple test) in Python: scikit_posthocs
Code tests around time in Python
Write C unit tests in Python
Write documentation in Sphinx with Python Livereload
Fourier series verification code written in Python
Stress Test with Locust written in Python
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
Write test-driven FizzBuzz code using Python doctest.
Isn't it okay to write test code?
Post Test 3 (Working with PosgreSQL in Python)
Write a short property definition in Python
Write O_SYNC file in C and Python
Write a Caesar cipher program in Python
Read and write JSON files in Python
Write a simple greedy algorithm in Python
Write python modules in fortran using f2py
Write a simple Vim Plugin in Python 3
Get the EDINET code list in Python
Notes on using code formatter in Python
How to write Ruby to_s in Python
python setup.py test the code using multiprocess
Quadtree in Python --2
Python in optimization
[Python / Selenium] XPath
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Write python-like code
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
python character code
nCr in python