Created
January 29, 2020 14:24
-
-
Save danizen/bd4bf6a560158c98b3b092ac780a76a4 to your computer and use it in GitHub Desktop.
Examples of Python NLM Selenium Test Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from nlm_selenium.seleniumtest import SeleniumTestCase | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support import expected_conditions | |
import pytest | |
from nlm_selenium.configuration import SeleniumSettings | |
@pytest.fixture(scope='module') | |
def selenium_settings(pytestconfig): | |
s = SeleniumSettings(pytestconfig) | |
# Set a URL for the test | |
s.baseUrl = 'http://google.com' | |
return s | |
class TestGoogleSearch(SeleniumTestCase): | |
def test_googlesearch(self): | |
# Open the page. Use the URL previously defined in the 'configureSelenium' method by 'setBaseUrl'. | |
self.openPage(self.baseUrl) | |
# Check the title. | |
self.titleShouldBe('Google') | |
# Find a text input element by its name. | |
search_field = self.findElement(By.NAME, 'q') | |
# Enter something to search for. | |
search_field.send_keys('National Library of Medicine') | |
# Submit the form with our search query. | |
search_field.submit() | |
# Wait for the page to load and check if the title is correct. | |
self.wait.until(expected_conditions.title_contains('National Library of Medicine')) | |
# Wait until the result are displayed. | |
self.wait.until(expected_conditions.presence_of_element_located((By.ID, 'resultStats'))) | |
# Check the first search result. | |
self.elementShouldContain(self.findElement(By.ID, 'rso'), | |
'National Library of Medicine - National Institutes of Health') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pytest | |
from nlm_selenium import SeleniumTestCase | |
from selenium.webdriver.common.by import By | |
class TestCas(SeleniumTestCase): | |
@pytest.mark.skip(reason='This test will only work if username and password are provided') | |
def test_wiki(self): | |
username = os.environ.get('my.username') | |
password = os.environ.get('my.password') | |
self.loginCasIfNeeded('https://wiki.nlm.nih.gov', username, password) # use 'loginCasIfNeeded' instead if login might not be required | |
self.titleShouldBe('Dashboard - NLM Wiki') | |
self.logoutCas((By.ID, 'user-menu-link'), (By.ID, 'logout-link')) # Arguments are elements to be clicked for logout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from nlm_selenium import SeleniumTestCase | |
from nlm_selenium.configuration import SeleniumSettings | |
from nlm_selenium.page_objects import PageObject, PageElement | |
@pytest.fixture(scope='module') | |
def selenium_settings(pytestconfig): | |
s = SeleniumSettings(pytestconfig) | |
s.baseUrl = 'https://ned.nih.gov' | |
return s | |
class SearchPage(PageObject): | |
URI = '/search/' | |
last_name = PageElement(id_='ContentPlaceHolder_txtLastName') | |
first_name = PageElement(id_='ContentPlaceHolder_txtFirstName') | |
search_button = PageElement(id_='ContentPlaceHolder_btnSearchName') | |
records_label = PageElement(id_='ContentPlaceHolder_lblRecords', caching=False) | |
error_label = PageElement(id_='ContentPlaceHolder_lblError', caching=False) | |
def should_have_no_results(self): | |
assert self.error_label | |
assert self.error_label.text | |
def should_have_results(self): | |
assert self.records_label | |
assert self.records_label.text | |
class TestNedSearch(SeleniumTestCase): | |
def test_wiring(self): | |
assert self.baseUrl == 'https://ned.nih.gov' | |
def test_ned_search_1(self): | |
# open | https:#ned.nih.gov/search/ | | |
page = SearchPage(self.driver, self.baseUrl) | |
page.open() | |
page.wait_for('first_name', 'last_name', 'search_button') | |
# type | id=ContentPlaceHolder_txtLastName | Paleontology | |
page.last_name.clear() | |
page.last_name.send_keys('Paleontology') | |
# type | id=ContentPlaceHolder_txtFirstName | John | |
page.first_name.clear() | |
page.first_name.send_keys('John') | |
# click | id=ContentPlaceHolder_btnSearchName | | |
page.search_button.click() | |
page.should_have_no_results() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment