-1

I would like to ask how to solve time delays in testing. I now have a script like this:

WebDriverWait (self.browser, 20) .until (EC.element_to_be_clickable ((By.ID, "Login1_LoginButton"))).
sleep (5) \ t
self.assertEqual ("Page 1 | Hotline", self.browser.title)
print ("Login OK")

It is a login to the application when the page name is checked after login. Due to the speed of the Internet it is not always optimal and sometimes the test fails. It would be better to put a condition there that would wait for the application to log in and then check. Also giving there just sleep (5) I find the wrong solution. Some idea?

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32
Tomasito
  • 306
  • 2
  • 15
  • what does this have to do with unit testing? – Andrei Dragotoniu Jun 21 '19 at 12:00
  • Does this answer your question? [How to use 'expected conditions' to check for an element in python-selenium?](https://stackoverflow.com/questions/47790010/how-to-use-expected-conditions-to-check-for-an-element-in-python-selenium) – Prophet Jun 08 '21 at 16:57

3 Answers3

3

Tomasito,

You have to figure out which element appears in last when successful login is done, then write the locator of that element and pass in web driver command. WebDriverwait is optimal solution for this :

after clicking on login button, you can assert page name like this :

wait = WebDriverWait(self.browser, 10)
element = wait.until(EC.visibility_of_element_located((By.ID, 'id of web page heading or any element that loads after')))  

self.assertEqual ("Page 1 | Hotline", self.browser.title)
print ("Login OK")

Note that The extreme case of this is time.sleep(), which sets the condition to an exact time period to wait.

You can refer official link

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

Set your wait condition as like this below it worked for me. if not working let me know. And also don't confuse with self.browser & self.assertEqual, i didn't use self because , this worked for me without creating a class. if you are creating a class and methods you should declare self variable. For My program i just created method and run and declared as global variable.like below,

def setup():
     global driver
     driver = webdriver.Chrome(executable_path="")
     driver.maximize_window()

def execute():

    wait = WebDriverWait(browser, 10)
    element = wait.until(EC.visibility_of_element_located((By.ID, 'id of web 
    page heading or any element that loads after')))  

    assertEqual ("Page 1 | Hotline", self.browser.title)
    print ("Login OK")
koushick
  • 497
  • 2
  • 8
  • 30
0

Some more information about the usecase would have helped us to constructed a more canonical answer. However you can consider the following approaches:

  • If your usecase is to assert the Page Title you can induce WebDriverWait for any element within the <body> tag e.g Page Heading, as by that mean time Page Title i.e. the <title> tag which is always a part of the <head> tag is bound to be rendered much before the elements within the <body> tag and you can use the following solution:

    WebDriverWait (self.browser, 20) .until (EC.visibility_of_element_located ((By.CSS_SELECTOR, "css_page_header")))
    self.assertEqual ("Page 1 | Hotline", self.browser.title)
    print ("Login OK")
    
  • You can also induce WebDriverWait for the Page Title to contain a certain text e.g. Hotline and then assert the complete Page Title as follows:

    WebDriverWait (self.browser, 20) .until (title_contains("Hotline"))
    self.assertEqual ("Page 1 | Hotline", self.browser.title)
    print ("Login OK")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352