Test automation with Selenium WebDriver and pytest
A brief about Selenium WebDriver
Selenium is an open-source tool that automates web application
testing across numerous platforms such as Windows, Mac, and Linux. With
Selenium, you can perform web testing against some of the popular
browsers like Chrome, Firefox, Opera, Microsoft Edge, etc. It also
supports various programming languages like Python, C#, Ruby, PERL,
Java, etc.
Selenium WebDriver is one of the core components of the Selenium
framework. It is a collection of open-source APIs that are used to
automate the testing of a web application. It allows test automation
to open a browser, then sends clicks, type keys, scrape text, and
finally exit the browser cleanly. There are particular drivers for the
browsers that include Chrome, Firefox, Opera, Microsoft Edge.
- driver = Chrome() - Chrome() initializes the ChromeDriver instance using the default options on your local machine.
- driver.implicitly_wait(10) - The challenging part
of web UI test automation is to wait for the page to load/change just
after firing an interaction as the page takes time to render new
elements. If this automation tries to access the new elements even
before they occur, WebDriver will raise a NoSuchElementException in this
scenario. It takes up to 10 seconds for elements to exist as the
implicity_wait method as mentioned above tells you to wait while trying
to find those elements. It is just once that these implicit waits are
declared before being used automatically for all elements.
- yield driver - A value should be returned by the
pytest fixture that represents the setup. To the initialized WebDriver
the fixture returns a reference. The WebDriver does not use a return
statement but uses yield which means the fixture is a generator.
In this case the first iteration of the fixture, the WebDriver
initialization is the “setup” phase that can be defined before a test
begins. The second iteration is to quit calling that denotes a “cleanup”
phase and can be used after a test finishes. When you write the
fixtures as generators it helps you to keep together the related setup
and cleanup operations as a single concern.
- driver.close() -Always
quit your WebDriver instance at the end of a test. When the test
automation ends it is not necessary the driver processes on the test
machine would always die. If you happen to fail in quitting a driver
instance it may keep on running as a zombie process that could consume
as well as lock the system resources.
Example code :
time.sleep(4)
Comments
Post a Comment