在下面的代码中,我无法运行第二个测试功能 def testclick(self)
import unittest
from selenium import webdriver
from time import sleep
class LoginMaharaOrgLogin(unittest.TestCase):
@classmethod
def setUpClass(inst):
inst.driver = webdriver.Chrome()
#inst.driver.maximize_window()
inst.driver.get("https://demo.mahara.org/")
def testlogin(self):
driver = self.driver
self.assertEqual("Home - Mahara Demo", driver.title)
driver.find_element_by_xpath("//input[@id='login_login_username']").send_keys("student")
driver.find_element_by_xpath("//input[@id='login_login_password']").send_keys("MaharaDemo")
driver.find_element_by_xpath("//input[@id='login_submit']").click()
driver.find_element_by_xpath("//span[@class='icon icon-chevron-down collapsed']").click()
sleep(3)
def testclick(self):
self.driver.find_element_by_xpath("//span[text()='Logout']").click()
@classmethod
def tearDownClass(inst):
inst.driver.close()
if __name__ == '__main__':
unittest.main()
当我运行上面的代码时,我得到错误:
Finding files... done.
Importing test modules ... done.
Hi
======================================================================
ERROR: testclick (Login.LoginMaharaOrgLogin)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\Keerthiaradhya\eclipse-workspace\Unit_Test\Login.py", line 25, in testclick
self.driver.find_element_by_xpath("//span[text()='Logout']").click()
File "C:\Users\Keerthiaradhya\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 365, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\Keerthiaradhya\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 855, in find_element
'value': value})['value']
File "C:\Users\Keerthiaradhya\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "C:\Users\Keerthiaradhya\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[text()='Logout']"}
(Session info: chrome=63.0.3239.84)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.16299 x86_64)
----------------------------------------------------------------------
Ran 2 tests in 18.020s
FAILED (errors=1)
但如果我包括这个:
self.driver.find_element_by_xpath("//span[text()='Logout']").click()
def testlogin(self)
中的语句有效
答案 0 :(得分:0)
你可以尝试:
def testclick(self):
driver.find_element_by_xpath("//span[text()='Logout']").click()
答案 1 :(得分:0)
unittest framework executes test functions based on the name of the function and not in the order that is placed. in your case its executing testclick() first & it's not finding the element. so use test1_login(self):
and test2_logout(self):
as function name it will work.