本文整理汇总了Python中selenium.webdriver.firefox.webdriver.WebDriver.find_element_by_xpath方法的典型用法代码示例。如果您正苦于以下问题:Python WebDriver.find_element_by_xpath方法的具体用法?Python WebDriver.find_element_by_xpath怎么用?Python WebDriver.find_element_by_xpath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类selenium.webdriver.firefox.webdriver.WebDriver
的用法示例。
在下文中一共展示了WebDriver.find_element_by_xpath方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BasicViewTests
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
class BasicViewTests(unittest.TestCase):
"""
Just visit all the views and make sure they look okay
"""
def setUp(self):
self.selenium = WebDriver()
self.live_server_url = LIVE_SERVER_URL
# login boilerplate blah
self.selenium.get(self.live_server_url + '/login')
username_input = self.selenium.find_element_by_name("username_or_email")
username_input.send_keys(ADMIN_USERNAME)
password_input = self.selenium.find_element_by_name("password")
password_input.send_keys(ADMIN_PASSWORD)
self.selenium.find_element_by_xpath('//button[@type="submit"]').click()
def test_home(self):
self.selenium.get(self.live_server_url + '/')
# two project links are there
self.selenium.find_element_by_link_text('1000 Genomes')
def test_project_home(self):
self.selenium.get(self.live_server_url + '/project/1kg')
def test_family_home(self):
self.selenium.get(self.live_server_url + '/project/1kg/family/HG1')
def tearDown(self):
self.selenium.quit()
示例2: BaseLiveServerTestCase
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
class BaseLiveServerTestCase(LiveServerTestCase):
def setUp(self):
get_or_create_test_admin()
self.browser = WebDriver()
self.do_admin_login('test', 'test')
def tearDown(self):
self.browser.quit()
def do_admin_login(self, username, password):
self.browser.get('%s%s' % (self.live_server_url, '/admin/'))
username_input = self.browser.find_element_by_name("username")
username_input.send_keys(username)
password_input = self.browser.find_element_by_name("password")
password_input.send_keys(password)
self.browser.find_element_by_xpath('//input[@value="Log in"]').click()
def new_window(self, name='other'):
self.browser.execute_script('window.open("/admin/", "'+ name +'")')
self.browser.switch_to_window(self.browser.window_handles[1])
while self.warning_element() is False:
time.sleep(0.1)
self.browser.switch_to_window(self.browser.window_handles[0])
def press_space(self):
a = ActionChains(self.browser)
a.key_down(Keys.SPACE)
a.perform()
def wait_for_pages_loaded(self):
for win in self.browser.window_handles:
while self.warning_element() is False:
time.sleep(0.1)
示例3: __init__
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
class Base:
@classmethod
def __init__(self, p_live_server_url):
self.live_server_url = p_live_server_url
self.driver = WebDriver()
@classmethod
def pause(self, p_seconds):
try:
WebDriverWait(self.driver, p_seconds).until(EC.title_is('pause'))
except:
pass
@classmethod
def grid_cell_doubleclick(self, p_element):
ActionChains(self.driver).double_click(p_element).perform()
@classmethod
def grid_cell_input(self, p_element, p_text):
self.grid_cell_doubleclick(p_element)
for k in range(0, len(p_element.text)):
p_element.send_keys(Keys.BACKSPACE)
p_element.send_keys(p_text)
@classmethod
def action_login(self, p_username, p_password, p_expectsuccess=True):
self.driver.get('{0}{1}'.format(self.live_server_url, '/login/'))
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
username_input = self.driver.find_element_by_id('txt_user')
username_input.send_keys(p_username)
password_input = self.driver.find_element_by_id('txt_pwd')
password_input.send_keys(p_password)
self.driver.find_element_by_xpath("//button[. = 'Sign in']").click()
if p_expectsuccess:
try:
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'header')))
except:
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, 'div_alert_text')))
else:
try:
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, 'div_alert_text')))
except:
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'header')))
@classmethod
def action_create_user(self, p_username, p_password, p_superuser):
self.driver.get('{0}{1}'.format(self.live_server_url, '/users/'))
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
assert 'OmniDB' in self.driver.title
self.driver.find_element_by_xpath("//button[. = 'New User']").click()
username_cell = self.driver.find_element_by_xpath("//tbody/tr[last()]/td[1]")
self.grid_cell_input(username_cell, p_username)
password_cell = self.driver.find_element_by_xpath("//tbody/tr[last()]/td[2]")
self.grid_cell_input(password_cell, p_password)
if p_superuser:
superuser_cell = self.driver.find_element_by_xpath("//tbody/tr[last()]/td[3]")
self.grid_cell_doubleclick(superuser_cell)
self.driver.find_element_by_tag_name('body').click()
self.driver.find_element_by_xpath("//button[. = 'Save Data']").click()
示例4: decline_offer
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
def decline_offer(offer_reference):
driver = WebDriver()
# Login
selenium_login(webdriver=driver)
# Decline offer
driver.get('https://www.odesk.com/applications/%s' % offer_reference)
driver.find_element_by_id("declineButton").click()
driver.find_element_by_css_selector("option[value=\"146\"]").click()
driver.find_element_by_id("otherReason").clear()
driver.find_element_by_id("otherReason").send_keys("Test")
driver.find_element_by_xpath("(//a[contains(text(),'Decline')])[2]").click()
driver.close()
示例5: TestGroup
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
class TestGroup(unittest.TestCase):
def setUp(self):
self.wd = WebDriver()
self.wd.implicitly_wait(60)
def test_create_group(self):
"""Validation of correct create test group"""
success = True
wd = self.wd
wd.get("http://localhost/addressbook/group.php")
wd.find_element_by_name("user").click()
wd.find_element_by_name("user").clear()
wd.find_element_by_name("user").send_keys("admin")
wd.find_element_by_id("LoginForm").click()
wd.find_element_by_name("pass").click()
wd.find_element_by_name("pass").clear()
wd.find_element_by_name("pass").send_keys("secret")
wd.find_element_by_css_selector("input[type=\"submit\"]").click()
wd.find_element_by_name("new").click()
wd.find_element_by_name("group_name").click()
wd.find_element_by_name("group_name").clear()
wd.find_element_by_name("group_name").send_keys("test")
wd.find_element_by_name("group_header").click()
wd.find_element_by_name("group_header").clear()
wd.find_element_by_name("group_header").send_keys("test")
wd.find_element_by_name("group_footer").click()
wd.find_element_by_name("group_footer").clear()
wd.find_element_by_name("group_footer").send_keys("test")
wd.find_element_by_name("submit").click()
wd.find_element_by_css_selector("div.msgbox").click()
wd.find_element_by_link_text("group page").click()
self.assertTrue(success)
def tearDown(self):
success = True
# We should delete all created test form
self.wd.find_element_by_link_text("groups").click()
self.wd.find_element_by_css_selector("span.group").click()
if not self.wd.find_element_by_name("selected[]").is_selected():
self.wd.find_element_by_name("selected[]").click()
self.wd.find_element_by_xpath("//div[@id='content']/form/input[5]").click()
self.wd.find_element_by_link_text("Logout").click()
self.assertTrue(success)
self.wd.quit()
示例6: AnonymousUseCaseTests
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
class AnonymousUseCaseTests(LiveServerTestCase):
fixtures = ['user']
@classmethod
def setUpClass(self):
self.selenium = WebDriver()
super(AnonymousUseCaseTests, self).setUpClass()
self.timeout = 10
@classmethod
def tearDownClass(self):
super(AnonymousUseCaseTests, self).tearDownClass()
self.selenium.quit()
def test_login(self):
self.selenium.get('%s%s' % (self.live_server_url, '/login/'))
self.assertEquals(self.selenium.title, "Squire Learning Application")
username_input = self.selenium.find_element_by_name("username")
username_input.send_keys('super')
password_input = self.selenium.find_element_by_name("password")
password_input.send_keys('super')
self.selenium.find_element_by_xpath('//input[@value="Login"]').click()
WebDriverWait(self.selenium, self.timeout).until(
lambda driver: driver.find_element_by_tag_name('body'))
self.assertEquals(self.selenium.current_url,
'%s%s' % (self.live_server_url, '/hr/profile/'))
def test_unsuccessful_login(self):
self.selenium.get('%s%s' % (self.live_server_url, '/login/'))
self.assertEquals(self.selenium.title, "Squire Learning Application")
username_input = self.selenium.find_element_by_name("username")
username_input.send_keys('super')
password_input = self.selenium.find_element_by_name("password")
password_input.send_keys('duper')
self.selenium.find_element_by_xpath('//input[@value="Login"]').click()
WebDriverWait(self.selenium, self.timeout).until(
lambda driver: driver.find_element_by_tag_name('body'))
self.assertEquals(self.selenium.current_url,
'%s%s' % (self.live_server_url, '/login/'))
self.assertTrue(
self.selenium.page_source.find("Please try again.") > 0)
示例7: EntrySeleleniumTests
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
class EntrySeleleniumTests(StaticLiveServerTestCase):
"""Selenium tests for the entry form"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if not settings.DEBUG:
settings.DEBUG = True
def setUp(self):
"""Handles login and things"""
call_command('flush', interactive=False, verbosity=0) # Clears db
call_command('loaddata', 'groups', commit=False, verbosity=0)
call_command('loaddata', 'school', commit=False, verbosity=0)
call_command('loaddata', 'permissions', commit=False, verbosity=0)
call_command('loaddata', 'auth_users', commit=False, verbosity=0)
call_command('loaddata', 'student', commit=False, verbosity=0)
call_command('loaddata', 'advisor', commit=False, verbosity=0)
call_command('loaddata', 'coordinator', commit=False, verbosity=0)
call_command('loaddata', 'activityoptions', commit=False, verbosity=0)
call_command('loaddata', 'learningobjectiveoptions', commit=False, verbosity=0)
call_command('loaddata', 'sample_entries', commit=False, verbosity=0)
self.selenium = WebDriver()
self.selenium.set_window_size(1024, 800)
self.selenium.get('{0}/{1}'.format(self.live_server_url, ''))
self.selenium.find_element_by_xpath('//*[@id="djHideToolBarButton"]').click()
self.selenium.implicitly_wait(10)
self.selenium.find_element_by_link_text('Login').click()
# Click on the student button in the gateway
self.selenium.find_element_by_xpath('/html/body/center/md-content/div/div/div[1]/a').click()
self.selenium\
.find_element_by_xpath("//img[@src='/static/journal/activities/img"
"/journal_sign.png']")
super()
def tearDown(self):
self.selenium.quit()
super()
def test_text_entry(self):
"""Test to ensure that a student can add a text entry"""
# Click on the first activity box: Walking around the block
self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click()
self.selenium.find_element_by_link_text('Add an entry').click()
# The following has 2 matching: Just walking and Adding entry...block
header_text = self.selenium.find_elements_by_tag_name('h3')[1].text
self.assertTrue('Adding entry for Walking around the block!' in header_text)
# Switching to iframe focus
self.selenium.switch_to_frame(self.selenium.find_element_by_id('id_entry_iframe'))
# Insert text
entry = 'I think I will bring my cat out next time.'
self.selenium.find_element_by_class_name('note-editable')\
.send_keys(entry)
# Switch back out of the iframe.
self.selenium.switch_to_default_content()
# Click on the submit button
self.selenium.find_element_by_class_name('btn-success').click()
# Ensure that we are back on the entries page.
self.selenium.find_element_by_link_text('Add an entry')
# Ensure that entry exists as the first box on the page.
box_text = self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div[1]/a/div').text
self.assertTrue(entry in box_text)
def test_image_entry(self):
"""Test to ensure that a student can add an image entry"""
# Click on the first activity box: Walking around the block
self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click()
self.selenium.find_element_by_link_text('Add an entry').click()
# The following has 2 matching: Just walking and Adding entry...block
header_text = self.selenium.find_elements_by_tag_name('h3')[1].text
self.assertTrue('Adding entry for Walking around the block!' in header_text)
# Switching to iframe focus
self.selenium.switch_to_frame(self.selenium.find_element_by_id('id_entry_iframe'))
self.selenium.find_element_by_xpath('/html/body/div[2]/div[5]/div[3]/button[2]').click()
entry = 'http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg'
self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/input')\
.send_keys(entry)
# click on the inset image button
self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[3]/button').click()
# Switch back out of the iframe.
self.selenium.switch_to_default_content()
# Click on the submit button
self.selenium.implicitly_wait(10)
self.selenium.find_element_by_class_name('btn-success').click()
# Ensure that we are back on the entries page.
self.selenium.find_element_by_link_text('Add an entry')
# Ensure that entry exists on the page.
self.selenium.find_element_by_xpath("//img[@src='http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg']")
def test_video_entry(self):
"""Test to ensure that a student can add a video entry"""
# Click on the first activity box: Walking around the block
self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click()
self.selenium.find_element_by_link_text('Add an entry').click()
# The following has 2 matching: Just walking and Adding entry...block
header_text = self.selenium.find_elements_by_tag_name('h3')[1].text
self.assertTrue('Adding entry for Walking around the block!' in header_text)
# Switching to iframe focus
self.selenium.switch_to_frame(self.selenium.find_element_by_id('id_entry_iframe'))
# Insert video
self.selenium.find_element_by_xpath('/html/body/div[2]/div[5]/div[3]/button[3]').click()
#.........这里部分代码省略.........
示例8: webHelper
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
class webHelper():
def __init__(self):
self.success = True
self.wd = WebDriver()
self.wd.implicitly_wait(60)
def setText(self, locator, text):
self.wd.find_element_by_xpath(locator).click()
self.wd.find_element_by_xpath(locator).clear()
self.wd.find_element_by_xpath(locator).send_keys(text)
def selectCheckbox(self, locator):
if not self.wd.find_element_by_xpath(locator).is_selected():
self.wd.find_element_by_xpath(locator).click()
def unselectCheckBox(self, locator):
if self.wd.find_element_by_xpath(locator).is_selected():
self.wd.find_element_by_xpath(locator).click()
def clickButton(self, locator):
self.wd.find_element_by_xpath(locator).click()
def open(self,locator):
self.wd.get(locator)
示例9: BaseStoreTest
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
class BaseStoreTest(StaticLiveServerTestCase):
def by_id(self, id):
return self.selenium.find_element_by_id(id)
def by_css(self, selector: str, multiple: bool = False):
if multiple:
return self.selenium.find_elements_by_css_selector(selector)
else:
return self.selenium.find_element_by_css_selector(selector)
def setUp(self):
self.selenium = WebDriver()
self.selenium.implicitly_wait(SELENIUM_WAIT_SEC)
user = create_user(TEST_USER, TEST_PASSWORD, TEST_EMAIL)
user.firstname = 'live'
user.lastname = 'test'
user.save()
def tearDown(self):
try:
delete_user(TEST_USER)
except Exception:
pass
super().tearDown()
self.selenium.quit()
def go_to(self, url_name: str, kwargs: Dict[str, str] = None) -> None:
app_url = reverse(url_name, kwargs=kwargs)
self.selenium.get('%s%s' % (self.live_server_url, app_url))
def go_to_app(self, app_id):
self.go_to('app-detail', {'id': app_id})
def go_to_app_register(self):
self.go_to('app-register')
def go_to_app_upload(self):
self.go_to('app-upload')
def go_to_login(self):
self.selenium.get('%s%s' % (self.live_server_url, '/login/'))
def login(self, user: str = TEST_USER, password: str = TEST_PASSWORD):
self.go_to_login()
user_input = self.selenium.find_element_by_name("login")
user_input.send_keys(user)
pass_input = self.selenium.find_element_by_name("password")
pass_input.send_keys(password)
self.selenium.find_element_by_xpath('//button[@type="submit"]').click()
def assert_can_not_login(self):
self.go_to('home')
self.go_to_login()
self.by_id('id_login').clear()
self.by_id('id_login').send_keys('livetest')
self.by_id('id_password').clear()
self.by_id('id_password').send_keys('livetest')
self.by_css('.auth-form button[type="submit"]').click()
error = self.by_css('.auth-form .text-danger')
self.assertTrue(error.is_displayed())
self.assertOnPage('account_login')
def logout(self):
self.findNavigationLink('account_logout').click()
def wait_for(self, selector: str,
then: Callable[[WebElement], None]) -> Any:
element = WebDriverWait(self.selenium, SELENIUM_WAIT_SEC).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, selector)))
return then(element)
def wait_for_url(self, url: str) -> Any:
WebDriverWait(self.selenium, SELENIUM_WAIT_SEC).until(
EC.url_contains(url)
)
def assertOnPage(self, url_name: str,
kwargs: Dict[str, str] = None) -> None:
parsed = urlparse(self.selenium.current_url)
url = reverse(url_name, kwargs=kwargs)
self.assertEqual(url, parsed.path)
def findNavigationLink(self, url_name: str,
kwargs: Dict[str, str] = None):
route = reverse(url_name, kwargs=kwargs)
return self.by_css('#navbar a[href="%s"]' % route)
示例10: is_alert_present
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
def is_alert_present(wd):
try:
wd.switch_to_alert().text
return True
except:
return False
try:
wd.get("https://rippletrade.com/")
wd.find_element_by_link_text("Sign Up").click()
wd.find_element_by_id("register_username").click()
wd.find_element_by_id("register_username").clear()
wd.find_element_by_id("register_username").send_keys("yuiyoxoq")
wd.find_element_by_id("register_password").click()
wd.find_element_by_id("register_password").clear()
wd.find_element_by_id("register_password").send_keys("b1zd3vf0r3v3r")
wd.find_element_by_id("register_password2").click()
wd.find_element_by_id("register_password2").clear()
wd.find_element_by_id("register_password2").send_keys("b1zd3vf0r3v3r")
wd.find_element_by_id("register_email").click()
wd.find_element_by_id("register_email").clear()
wd.find_element_by_id("register_email").send_keys("[email protected]")
if not wd.find_element_by_id("terms").is_selected():
wd.find_element_by_id("terms").click()
wd.find_element_by_xpath("//div[@id='t-register']//button[.='Sign UpMigrate Account']").click()
wd.find_element_by_xpath("//div[@id='t-register']/section/div[4]/div[2]/div[5]/button").click()
finally:
wd.quit()
if not success:
raise Exception("Test failed.")
示例11: is_alert_present
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
def is_alert_present(wd):
try:
wd.switch_to_alert().text
return True
except:
return False
try:
wd.get("http://localhost/addressbook/index.php")
wd.find_element_by_name("user").click()
wd.find_element_by_name("user").clear()
wd.find_element_by_name("user").send_keys("admin")
wd.find_element_by_name("pass").click()
wd.find_element_by_name("pass").clear()
wd.find_element_by_name("pass").send_keys("secret")
wd.find_element_by_xpath("//form[@id='LoginForm']/input[3]").click()
wd.find_element_by_link_text("groups").click()
wd.find_element_by_name("new").click()
wd.find_element_by_name("group_name").click()
wd.find_element_by_name("group_name").clear()
wd.find_element_by_name("group_name").send_keys("adfadf")
wd.find_element_by_name("group_header").click()
wd.find_element_by_name("group_header").clear()
wd.find_element_by_name("group_header").send_keys("adfadfadf")
wd.find_element_by_name("group_footer").click()
wd.find_element_by_name("group_footer").clear()
wd.find_element_by_name("group_footer").send_keys("adfadfdf")
wd.find_element_by_name("submit").click()
wd.find_element_by_link_text("groups").click()
wd.find_element_by_link_text("Logout").click()
wd.find_element_by_name("user").click()
示例12: WebDriver
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
from selenium.webdriver.firefox.webdriver import WebDriver
wd = WebDriver()
wd.implicitly_wait(60)
wd.get("https://encrypted.google.com/")
wd.find_element_by_id("gbqfq").click()
wd.find_element_by_id("gbqfq").clear()
wd.find_element_by_id("gbqfq").send_keys("santiago suarez ordoñez")
wd.find_element_by_xpath("//ol[@id='rso']//em[.='Santiago Suarez Ordoñez']").click()
wd.close()
示例13: ExampleCommunityLiveTests
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
class ExampleCommunityLiveTests(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
super(ExampleCommunityLiveTests, cls).setUpClass()
@classmethod
def tearDownClass(cls):
# cls.selenium.quit()
super(ExampleCommunityLiveTests, cls).tearDownClass()
def tearDown(self):
self.selenium.get(self.full_url(reverse('logout')))
self.selenium.quit()
def setUp(self):
self.community = Community.objects.create(
name="Kibbutz Broken Dream",
)
self.users_details = dict()
for i in range(NUM_OF_USERS):
name = silly.name(slugify=True)
email = silly.email()
self.users_details[name] = OCUser.objects.create_superuser(email, name, DEFAULT_PASS)
self.committee = Committee.objects.create(name="Culture", slug="culture", community=self.community)
self.selenium = WebDriver()
def full_url(self, s):
if s.startswith(self.live_server_url):
return s
else:
return self.live_server_url + s
def get_current_path(self):
return urlparse.urlsplit(self.selenium.current_url).path
def assert_current_path(self, path):
self.assertEqual(self.full_url(path), self.full_url(self.get_current_path()))
def selenium_get_and_assert(self, url):
"""
Tries to go url (as is) and then asserts that current_path == url
"""
self.selenium.get(url)
self.assert_current_path(url)
def login(self, goto_login, name, pswd=DEFAULT_PASS):
if goto_login is True:
self.selenium.get(self.full_url(reverse('login')))
self.assert_current_path(reverse('login'))
username_input = self.selenium.find_element_by_id("id_username")
username_input.send_keys(self.users_details[name].email)
password_input = self.selenium.find_element_by_id("id_password")
password_input.send_keys(pswd)
self.selenium.find_element_by_xpath('//input[@type="submit"]').click()
def test_redirect_and_login(self):
url = self.full_url(self.community.get_absolute_url())
# from IPython import embed
# embed()
self.selenium.get(url)
name = random.choice(self.users_details.keys())
self.login(False,
name) # False since the community we created is private - it should automatically redircet to login
def test_create_meeting(self):
name = random.choice(self.users_details.keys())
self.login(True, name)
"""
TODO - after login goto the community page, locate the relevant html element for the committee - and 'click()' it
(The below code didn't work because click didn't work for me - idk y
url = self.full_url(self.community.get_absolute_url())
self.selenium_get_and_assert(url)
self.selenium.find_element_by_class_name(
"panel-heading").click() # goto "Next meeting" (TODO: check if this works if we have single \ multiple meetings in the system)
self.assert_current_path(
self.committee.get_absolute_url()) # note: we're seeing a new meeting in progress - hence its' url is the committees'
"""
url = self.full_url(self.committee.get_absolute_url())
self.selenium_get_and_assert(url)
# TODO: assert that we're seeing all the communities we're a member of
new_subject_input = self.selenium.find_element_by_id("quick-issue-title")
new_subject_input.send_keys("dummy-subject1")
self.selenium.find_element_by_xpath(
'//button[@type="submit"]').click() # should behave exactly like: self.selenium.find_element_by_id("quick-issue-add").click()
# Create a new committee & assert what's needed
示例14: SeleniumTests
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
class SeleniumTests(LiveServerTestCase):
"""Base class for all Selenium test classes to inherit from"""
@classmethod
def setUpClass(self):
self.selenium = WebDriver()
self.selenium.implicitly_wait(3)
super(SeleniumTests, self).setUpClass()
@classmethod
def tearDownClass(self):
self.selenium.quit()
super(SeleniumTests, self).tearDownClass()
def setUp(self):
# Create a test user and admin account
self.user_name = 'SeleniumTestUser'
self.user_pass = 'test'
if User.objects.filter(username=self.user_name).count() == 0:
User.objects.create_user(self.user_name, '', self.user_pass).save()
self.admin_name = 'SeleniumAdministrativeUser'
self.admin_pass = 'test'
if User.objects.filter(username=self.admin_name).count() == 0:
User.objects.create_superuser(self.admin_name, '', self.admin_pass).save()
def tearDown(self):
self.selenium.delete_all_cookies()
def link_text_exists(self, link_text):
try:
self.selenium.find_element_by_link_text(link_text)
except NoSuchElementException:
return False
return True
def xpath_exists(self, xpath):
try:
self.selenium.find_element_by_xpath(xpath)
except NoSuchElementException:
return False
return True
def element_with_selector_exists(self, selector):
try:
self.selenium.find_element_by_css_selector(selector)
except NoSuchElementException:
return False
return True
def text_exists(self, text, xpath="//body"):
try:
element = self.selenium.find_element_by_xpath(xpath)
except NoSuchElementException:
return False
if text in element.text:
return True
else:
return False
def login(self, username, password):
# Go to the login page and try logging in with the provided credentials
self.selenium.get('%s' % self.live_server_url)
self.selenium.find_element_by_link_text('Log in').click()
username_input = self.selenium.find_element_by_name('username')
username_input.send_keys(username)
password_input = self.selenium.find_element_by_name('password')
password_input.send_keys(password)
self.selenium.find_element_by_class_name('submit').click()
def create_items_and_requests(self, x):
# Creates x number of items and requests
for i in xrange(x):
self.item = G(Item)
self.request = G(Request, item=self.item)
def create_generic_item(self):
# Quick helper in the absence of fixtures
item = G(Item, local_num=9999, part_class='parts class',
location='cart 8', description='test item', cfi='never',
company='A Business', part_num='X9X9', serial_num='8A',
asset_num='sample_num', notes='Testing')
return item
示例15: WebDriverWait
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import find_element_by_xpath [as 别名]
driver.find_element_by_link_text("Shop Repair Parts Now »").click()
# This try statement is used to detect the Foresee overlay that pops up at this point in FF
# Without this, Chrome will not continue to next step as it will error out while waiting
try:
wait = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.LINK_TEXT, "No, thanks")))
driver.find_element_by_link_text("No, thanks").click()
except Exception, e:
print("Foresee not seen here")
WebDriverWait(driver, 10).until_not(EC.presence_of_element_located((By.ID, "fsrOverlay")))
driver.find_element_by_id("aarisearch_brands_jl").click()
wait = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//ul[@class='ari-caused-spacer-expand']/li[3]")))
driver.find_element_by_xpath("//ul[@class='ari-caused-spacer-expand']/li[3]").click()
driver.find_element_by_id("arisearch_txtSearch").click()
driver.find_element_by_id("arisearch_txtSearch").clear()
driver.find_element_by_id("arisearch_txtSearch").send_keys("209617GS")
driver.find_element_by_id("arisearch_btnLookup").click()
# The following div classes get in the way of our user click as seen in errors
# and we must change the css with javascript to allow the button click without a sleep statement
# <div class="blockUI blockOverlay" ... wait; position: absolute;"></div>
try:
hideElement = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "blockUI")))
except Exception, e:
print("BlockUI not found")
try:
driver.execute_script("document.getElementsByClassName('blockUI blockOverlay')[0].style.height = '1px';")