第一次运行PyCharm ChromeDriver测试

时间:2018-02-14 19:17:27

标签: python selenium

所以我有Python 2.7.10。我有Selenium 3.5.9。我有当前版本的PyCharm,我设置了本地解释器。我试图在我公司的网站上运行一个unitest,然后我回来了:

  

在0.000秒内进行0次测试

     

这是我的代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest

class LoginTest(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.rosoka.com/")

    def test_Login(self):
        driver = self.driver
        rosokaUsername = "*****@rosoka.com"
        rosokaPassword = "******"
        loginFieldID = "edit-name"
        passFieldID = "edit-pass"
        loginButtonXpath = "//input[@value ='Log in']"

        loginFieldElement = WebDriverWait(driver, 10).until(lambda driver: find_element_by_id(loginFieldID))
        passFieldElement = WebDriverWait(driver, 10).until(lambda driver: find_element_by_id(passFieldID))
        loginButtonElement = WebDriverWait(driver, 10).until(lambda driver: find_element_by_xpath(loginButtonXpath))

        loginFieldElement.clear()
        loginFieldElement.send_keys(rosokaUsername)
        passFieldElement.clear()
        passFieldElement.send_keys(rosokapassword)
        loginButtonElement.click()

    def tearDown(self):
        self.driver.quit()

    if __name__ == '__main__':
        unittest.main()

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

if __name__ == '__main__'块需要缩进。它目前在你班级内。

此外,它似乎应该是rosokaPassword内的passFieldElement.send_keys(rosokapassword)(camelCase)。

编辑:

我修改了代码并在网站上进行了测试(虽然我没有真正的凭据,所以我不得不把剩下的留给你)。 Selenium在找到这些元素时遇到了麻烦,直到我徘徊在" User Portal"链接以显示下拉列表。

import unittest

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


class LoginTest(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.rosoka.com/")

    def test_login(self):
        rosoka_username = "*****@rosoka.com"
        rosoka_password = "******"

        user_portal = self.driver.find_element_by_xpath(
            '//*[@id="om-leaf-om-u1-570991293-8"]/span/span')
        hover = ActionChains(self.driver).move_to_element(user_portal)
        hover.perform()

        login_field = self.driver.find_element_by_id('edit-name')
        login_field.clear()
        login_field.send_keys(rosoka_username)

        pass_field = self.driver.find_element_by_id('edit-pass')
        pass_field.clear()
        pass_field.send_keys(rosoka_password)

        login_button = self.driver.find_element_by_id('edit-submit')
        login_button.click()

    def tearDown(self):
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()

答案 1 :(得分:0)

这是正确的答案,增加了等待时间和截图: 进口单位测试 进口时间

来自selenium import webdriver 来自selenium.webdriver.common.action_chains导入ActionChains

class LoginTest(unittest.TestCase):

def setUp(self):
    self.driver = webdriver.Chrome()
    self.driver.get("https://www.rosoka.com")
    self.driver.save_screenshot('rosoka.png')
    time.sleep(10)

def test_login(self):
    rosoka_username = "****"
    rosoka_password = "***"

    user_portal = self.driver.find_element_by_xpath('//*[@id="om-leaf-om-u1-570991293-8"]/span/span')
    hover = ActionChains(self.driver).move_to_element(user_portal)
    hover.perform()

    login_field = self.driver.find_element_by_id('edit-name')
    login_field.clear()
    login_field.send_keys(rosoka_username)

    pass_field = self.driver.find_element_by_id('edit-pass')
    pass_field.clear()
    pass_field.send_keys(rosoka_password)

    login_button = self.driver.find_element_by_id('edit-submit')
    login_button.click()
    time.sleep(10)


def tearDown(self):
    self.driver.quit()

如果名称 ==' 主要':     unittest.main()