dsmith
dsmith

Reputation: 37

First time running PyCharm ChromeDriver test

So I have Python 2.7.10. I have Selenium 3.5.9. I have the current version of PyCharm and I set up the local interpreter. I am trying to run a unitest on my company's website and I am getting back:

Ran 0 tests in 0.000s

OK

This is my code:

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()

Can someone help?

Upvotes: 0

Views: 193

Answers (2)

dsmith
dsmith

Reputation: 37

This is the correct answer with the addition of wait time and screenshot: import unittest import time

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")
    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()

if name == 'main': unittest.main()

Upvotes: 0

G_M
G_M

Reputation: 3372

The if __name__ == '__main__' block needs to be dedented. It is currently inside of your class.

Also, it looks like it should be rosokaPassword (camelCase) inside of passFieldElement.send_keys(rosokapassword).

EDIT:

I modified the code and tested it on the website (although I don't have real credentials so I'll have to leave the rest to you). Selenium was having trouble finding the elements until I hovered over the "User Portal" link to make the dropdown appear.

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()

Upvotes: 1

Related Questions