selenium icon indicating copy to clipboard operation
selenium copied to clipboard

Unable to reproduce user behavior with selenium on Python

Open dsgon opened this issue 5 years ago • 5 comments

🐛 Bug Report

Hi.

I am trying to register a user in this practice site: http://practice.automationtesting.in/my-account/

The process is very simple, write a user-email, write a password and then click on register button.

As user, you manually do the steps before describe and you could register a user. but if you do with selenium, you can't do it.

I discovered that manually you have a message about strong or weak password, and this message doesn't appear with selenium. But there are more. If I use record and play process with selenium IDE, I can reproduce the user behave, as I show in the video.

https://user-images.githubusercontent.com/37479902/120584864-1c16bf00-c407-11eb-914a-48fa00e22817.mov

I tried with Chrome and Firefox, but the result was the same.

So, I share technical information.

OS: Windows 10 x64
Python: 3.9.3
Pytest: 6.2.4
Selenium: 3.141.0
Chrome:  91.0.4472.77
Firefox: 89.0
Selenium IDE: 3.17.0

I also share the Python script:

import pytest
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import  expected_conditions as ec


driver = None
def setup():
    global driver
    chrome_driver_path = #path to chrome driver
    gecko_driver_path =  #path to gecko driver
    url = 'http://practice.automationtesting.in/my-account/'

    browser = 'chrome'
    if browser == 'chrome':
        driver = webdriver.Chrome(executable_path=chrome_driver_path)
    elif browser == 'firefox':
        driver = webdriver.Firefox(executable_path=gecko_driver_path)
    else:
        raise ValueError('Invalid Browser')

    driver.maximize_window()
    driver.set_page_load_timeout(10)
    driver.implicitly_wait(5)
    try:
        driver.get(url)
    except TimeoutException:
        print('Page not load')
        driver.quit()

def test_register_user():
    test_data = {
        'username' : '[email protected]',
        'password' : 'StR0nGp4AsSw0Rd'
    }
    driver.find_element_by_id('reg_email').send_keys(test_data['username'])
    driver.find_element_by_id('reg_password').send_keys(test_data['password'])
    wait_driver = WebDriverWait(driver,5,0.2)
    try:
        button = wait_driver.until(ec.element_to_be_clickable((By.NAME,'register')))
        button.click()
    except TimeoutException:
        pytest.fail('Register button is not clickable')


def teardown():
    driver.quit()

To execute the script I use:

python -m pytest path\to\script.py

What could be happening?

dsgon avatar Jun 03 '21 05:06 dsgon

💬 It looks like this is a question rather than an issue. 💡 A better way to address this is:

diemol avatar Jun 03 '21 13:06 diemol

it's not a question @diemol it's a issue, because selenium doesn't reproduce user behave.

Why do you close this?

dsgon avatar Jun 03 '21 15:06 dsgon

Maybe you can explain why it is an issue? Currently the IDE injects JS into the browser to execute some commands, so it does not work exactly the same as a Selenium WebDriver script. It is not and apples to apples comparison.

Also:

  • What do you mean by "user behaviour"?
  • In the Python script you are trying to assert for a button to be clickable, the IDE test is not doing that.

All in all, this seems to be a question because there is an expectation of two different tools doing the same thing when the underlying commands are different and also the flows are doing different things.

Which is why I invited you to join us in the Slack/IRC channel, so we can chat about that and the behaviour you expect.

diemol avatar Jun 04 '21 10:06 diemol

I will explain to you @diemol

It's very easy. If you try reproduce the User steps or User Behave (I guess that you didn't) after write password you will see a message with the Weak/Strong password and if you write a Strong password the Register button will be enable to click.

If you try create a Python Script with this steps, the message Weak/Strong password never is shown, for this reason the Register button never will be enable to click.

And if you create the same script into Java (for example), during the sendKeys process to write the password into the input field, you will see the message and the Register button will be enable (If you write a Strong Password).

So... it's a ISSUE for Selenium in Python

dsgon avatar Jun 04 '21 13:06 dsgon

The issue lays on the website under test, if you check the page source, it is full of JS scripts at the end that need quite a while to load. Specifically, for password checks, this script is used. If the JS code has not loaded and you try to interact with the input fields, you won't see the behaviour you are expecting.

JS code will take a while to load because every test executed with WebDriver it is using a fresh profile, where nothing has been cached. The IDE is using the current browser profile and session, which is why all that JS loads faster (cached).

I tried your script and I don't think it has any issue, it is just that the page you want to test is taking too long to load. To evidentiate that, I added a "only for demo purposes" sleep, and then you can see how the code works well. If you want to keep learning on this website, I would suggest to contact the owners to improve the site's performance and make it more testable, or to find a specific condition you can wait for before interacting with the website.

In the end, I believe this is not an issue with Selenium, rather with the website under test.

import pytest
import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import  expected_conditions as ec


driver = None
def setup():
    global driver
    chrome_driver_path = '/usr/local/bin/chromedriver'
    gecko_driver_path = '/usr/local/bin/geckodriver'
    url = 'http://practice.automationtesting.in/my-account/'

    browser = 'chrome'
    if browser == 'chrome':
        driver = webdriver.Chrome(executable_path=chrome_driver_path)
    elif browser == 'firefox':
        driver = webdriver.Firefox(executable_path=gecko_driver_path)
    else:
        raise ValueError('Invalid Browser')

    driver.maximize_window()
    driver.set_page_load_timeout(10)
    # Do not mix implicit and explicit waits
    # driver.implicitly_wait(5)
    try:
        driver.get(url)
    except TimeoutException:
        print('Page not load')
        driver.quit()


def test_register_user():
    test_data = {
        'username' : '[email protected]',
        'password' : 'StR0nGp4AsSw0Rd'
    }
    # Page is full of css and js, find what you want to wait for to be present
    # before inreacting with the script, so validations are shown as expected.
    # As a workaround to show the issue is in the website, not in Selenium, and old fashioned
    # sleep to let all that JS load before interacting with the page.
    time.sleep(20)
    driver.find_element_by_id('reg_email').send_keys(test_data['username'])
    driver.find_element_by_id('reg_password').send_keys(test_data['password'])
    wait_driver = WebDriverWait(driver, 5, 0.2)
    try:
        button = wait_driver.until(ec.element_to_be_clickable((By.NAME,'register')))
        button.click()
    except TimeoutException:
        pytest.fail('Register button is not clickable')


def teardown():
    driver.quit()

If you have any more questions, please join us at our IRC/Slack channel.

diemol avatar Jun 07 '21 13:06 diemol