Releases: seleniumbase/SeleniumBase
Add mobile device testing to SeleniumBase
Add mobile device testing to SeleniumBase
Use --mobile
to quickly run your tests using Chrome's mobile device emulator with default values for device metrics (CSS Width, CSS Height, Pixel-Ratio) and a default value set for the user agent. To configure the mobile device metrics, use --metrics="CSS_Width,CSS_Height,Pixel_Ratio"
to set those values. You'll also be able to set the user agent with --agent="USER-AGENT-STRING"
(a default user agent will be used if not specified). To find real values for device metrics, see this GitHub Gist. For a list of available user agent strings, check out this page.
# Run tests using Chrome's mobile device emulator (default settings)
pytest test_swag_labs.py --mobile
# Run mobile tests specifying CSS Width, CSS Height, and Pixel-Ratio
pytest test_swag_labs.py --mobile --metrics="411,731,3"
# Run mobile tests specifying the user agent
pytest test_swag_labs.py --mobile --agent="Mozilla/5.0 (Linux; Android 9; Pixel 3 XL)"
Update requirements, ad-block settings, & methods
Update requirements, ad-blocking settings, and methods
- Update multiple Python requirements/dependencies
- Update default ad-blocking settings when using
--ad-block
- Add built-in
sleep()
method, which callstime.sleep()
Add option to reuse browser session between tests
Add an option to reuse the browser session between tests
-
Usage:
--reuse_session
or--reuse-session
This will keep the main browser window open betweenpytest
tests so that the Selenium session can be shared from one test to another. (This feature may be useful in some cases, such as tests that require logging in every time: With this feature enabled, SeleniumBase tests would only need to login once, because the session would be kept into the next test.) -
Also update some Python dependencies:
setuptools
,certifi
,pygments
,tqdm
-
Also update default timeout values when using the SeleniumBase Selenium Grid.
Support Chinese characters in PDF testing
Support Chinese characters in PDF testing
Here's the example test:
https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_chinese_pdf.py
# -*- coding: utf-8 -*-
from seleniumbase import BaseCase
class ChinesePdfTestClass(BaseCase):
def test_chinese_pdf(self):
pdf = ('https://github.com/seleniumbase/SeleniumBase/'
'files/3895614/unittest.pdf')
# Get and print PDF text
pdf_text = self.get_pdf_text(pdf, page=2)
print("\n" + pdf_text)
# Assert PDF contains the expected text on Page 2
self.assert_pdf_text(pdf, "个测试类", page=2)
# Assert PDF contains the expected text on any of the pages
self.assert_pdf_text(pdf, "运行单元测试")
self.assert_pdf_text(pdf, "等待测试结束后显示所有结果")
self.assert_pdf_text(pdf, "测试的执行跟方法的顺序没有关系")
- Updated methods:
def get_pdf_text(self, pdf, page=None, maxpages=None,
password=None, codec='utf-8', wrap=False, nav=False,
override=False):
""" Gets text from a PDF file.
PDF can be either a URL or a file path on the local file system.
@Params
pdf - The URL or file path of the PDF file.
page - The page number (or a list of page numbers) of the PDF.
If a page number is provided, looks only at that page.
(1 is the first page, 2 is the second page, etc.)
If no page number is provided, returns all PDF text.
maxpages - Instead of providing a page number, you can provide
the number of pages to use from the beginning.
password - If the PDF is password-protected, enter it here.
codec - The compression format for character encoding.
(The default codec used by this method is 'utf-8'.)
wrap - Replaces ' \n' with ' ' so that individual sentences
from a PDF don't get broken up into seperate lines when
getting converted into text format.
nav - If PDF is a URL, navigates to the URL in the browser first.
(Not needed because the PDF will be downloaded anyway.)
override - If the PDF file to be downloaded already exists in the
downloaded_files/ folder, that PDF will be used
instead of downloading it again. """
def assert_pdf_text(self, pdf, text, page=None, maxpages=None,
password=None, codec='utf-8', wrap=True, nav=False,
override=False):
""" Asserts text in a PDF file.
PDF can be either a URL or a file path on the local file system.
@Params
pdf - The URL or file path of the PDF file.
text - The expected text to verify in the PDF.
page - The page number of the PDF to use (optional).
If a page number is provided, looks only at that page.
(1 is the first page, 2 is the second page, etc.)
If no page number is provided, looks at all the pages.
maxpages - Instead of providing a page number, you can provide
the number of pages to use from the beginning.
password - If the PDF is password-protected, enter it here.
codec - The compression format for character encoding.
(The default codec used by this method is 'utf-8'.)
wrap - Replaces ' \n' with ' ' so that individual sentences
from a PDF don't get broken up into seperate lines when
getting converted into text format.
nav - If PDF is a URL, navigates to the URL in the browser first.
(Not needed because the PDF will be downloaded anyway.)
override - If the PDF file to be downloaded already exists in the
downloaded_files/ folder, that PDF will be used
instead of downloading it again. """
Use pytest>=5.3.1 for Python version >= 3
Use pytest>=5.3.1 for Python version >= 3
Add click_nth_visible_element(selector, number)
Add click_nth_visible_element(selector, number, by=By.CSS_SELECTOR)
Finds all matching page elements and clicks the nth visible one.
Example:
self.click_nth_visible_element('[type="checkbox"]', 5)
### (Clicks the 5th visible checkbox on the page.)
Add get_pdf_text() and use that in assert_pdf_text()
Add get_pdf_text()
and use that in assert_pdf_text()
-
get_pdf_text(pdf, page=None)
Gets the text from a PDF (The PDF can be either a URL or a path on the local file system. If a Page number is provided, uses that page, otherwise gets everything.) -
assert_pdf_text(pdf, text, page=None)
Asserts that certain text appears in a PDF (The PDF can be either a URL or a path on the local file system. If a Page number is provided, only checks that page, otherwise all pages get scanned for the expected text.)
Create the downloads_folder for PDF-testing use
Create the downloads_folder
for PDF-testing use
- When verifying text in a PDF that’s on the web, SeleniumBase will first download it to the
downloads_folder
. From there, it's easier to scan with Python for verification. - Continuation of v1.33.2 -> Add the
assert_pdf_text()
method for asserting text in a PDF file
Add methods for handling cookies on web pages
Add methods for handling cookies on web pages
self.save_cookies(name="cookies.txt")
self.load_cookies(name="cookies.txt")
self.delete_all_cookies()
self.delete_saved_cookies(name="cookies.txt")
(Cookies get saved to the "./saved_cookies/" folder.)