PAGE_TS001.py:

import json from selenium import webdriver class DemoQA(): def __init__(self): self.driver = webdriver.Firefox() with open("conf.json") as data_file: parser = json.load(data_file) url = parser.get("URL") self.driver = webdriver.Firefox() self.driver.get(url) #находим нужные поля fName = self.driver.find_element_by_id("name_3_firstname") lName = self.driver.find_element_by_id("name_3_lastname") phone = self.driver.find_element_by_id("phone_9") hobby = self.driver.find_element_by_name("checkbox_5[]") uName = self.driver.find_element_by_id("username") email = self.driver.find_element_by_id("email_1") password = self.driver.find_element_by_id("password_2") confirm = self.driver.find_element_by_id("confirm_password_password_2") submit = self.driver.find_element_by_name("pie_submit") #заполняем fName.send_keys(parser.get("First Name")) lName.send_keys(parser.get("Last Name")) hobby.click() phone.send_keys(parser.get("Phone Number")) uName.send_keys(parser.get("Username")) email.send_keys(parser.get("E-mail")) password.send_keys(parser.get("Password")) confirm.send_keys(parser.get("Confirm Password")) # кликаем сабмит submit.click() # hz nado/net zhdat` # driver.implicitly_wait(5) ts = DemoQA() 

TEST_TS001.py

 import pytest import page_TS001 def test_function(): text = "Username already exists" assert (text in page_TS001.ts.driver.find_elements_by_class_name("piereg_login_error") == True) test_function() 

I’m picking up testing, there are two files in the page and test projects, respectively, I’m not sure if I wrote the test correctly, just like I’m not sure that I created the project architecture (my testing knowledge is about 5 hours).

At the same time I have at launch Test_TS001.py: https://pastebin.com/6exM1E4T

 test_case_TS001.py:6: in test_function assert (text in page_TS001.ts.driver.find_elements_by_class_name("piereg_login_error") == True) E assert 'Username already exists' in [<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="c5d20ee4-8285-49fb-a777-42862e1c3afa", element="90b84448-6b35-4133-8925-e0b8bb2c0503")>] E + where [<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="c5d20ee4-8285-49fb-a777-42862e1c3afa", element="90b84448-6b35-4133-8925-e0b8bb2c0503")>] = <bound method WebDriver.find_elements_by_class_name of <selenium.webdriver.firefox.webdriver.WebDriver (session="c5d20ee4-8285-49fb-a777-42862e1c3afa")>>('piereg_login_error') E + where <bound method WebDriver.find_elements_by_class_name of <selenium.webdriver.firefox.webdriver.WebDriver (session="c5d20ee4-8285-49fb-a777-42862e1c3afa")>> = <selenium.webdriver.firefox.webdriver.WebDriver (session="c5d20ee4-8285-49fb-a777-42862e1c3afa")>.find_elements_by_class_name E + where <selenium.webdriver.firefox.webdriver.WebDriver (session="c5d20ee4-8285-49fb-a777-42862e1c3afa")> = <page_TS001.DemoQA object at 0x036D4930>.driver E + where <page_TS001.DemoQA object at 0x036D4930> = page_TS001.ts !!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!! ========================== 1 error in 13.07 seconds =========================== 

Test data in the conf.json file:

 { "URL":"http://demoqa.com/registration/", "First Name": "David", "Last Name": "Jones", "Hobby": "0", "Phone Number": "89377005211", "Username": "DavidJonesZZ", "E-mail": "johndoe@host.net", "Password": "mysecret", "Confirm Password": "mysecret" } 

Question: Why does this test not work? (I check for text in the field with the class "piereg_login_error"). Or do you need to use re?

ps. I also tried driver.page_source instead of a specific field - the same error.

pps. If it is easy to point out inaccuracies in the work.

  • one
    You would not hurt to give a minimal, self-sufficient and reproducible example , because the code from the question does not work for anyone (after all, nobody has your website and the test has nothing to test with other people), which greatly reduces the chances of getting help, because without A reproducible example of an answer to a question often turns into fortune telling, and not everyone likes it. - andreymal
  • @andreymal, yes, that's right, this is my omission: corrected a question, added source data This is constructive criticism! Thank you - p4sh

2 answers 2

find_elements_by_class_name returns a list of items. If you know that you have exactly one such element on a page, then use find_element_by_class_name . And to get the text inside the element, you need to use the text attribute. To make it work, your check should be written as follows:

 assert text in page_TS001.ts.driver.find_element_by_class_name("piereg_login_error").text 

But this is only a local solution ... Your fear

what normally created the project architecture

rightly so. What would I change:

  1. Remove class object from the PAGE_TS001.py of PAGE_TS001.py
  2. In __init__() leave only the driver initialization
  3. Config parsing is generally done in a separate file (to separate the logic and data). Usually called data_providers.py .
  4. Registration steps need to be done in the test itself, but without searching for controls. Ideally, it is still possible to take out the logic separately, but this can be done at later stages of learning to write autotests.

In general, the test should look something like this:

 import page_TS001 import data_providers def test_function(): application = page_TS001.DemoQA() reg_page = application.open_registration_page() reg_page.enter_f_name(data_providers.first_name) reg_page.enter_l_name(data_providers.last_name) # ... ввод остального ... reg_page.click_submit() assert data_providers.text == reg_page.get_notification() 

I also recommend exploring explicit expectations and inserting them in the right places in the methods of the DemoQA class DemoQA

    I will answer myself, because figured out:

     assert (text in page_TS001.ts.driver.find_elements_by_class_name("piereg_login_error") == True) 

    need to change to

     assert (text in page_TS001.ts.driver.page_source ) 
    • one
      Checking the presence of text at once in the entire source code of the page is an idea so-so, because the text may not be in piereg_login_error , but in some other place, as a result of which the test will check not what it should check. I recommend not to insult about the fact that for some unfortunate two hours there were no specialists on Selenium who could answer your question (people of work and family, you know, they don’t sit around ruSO), but wait a day -other, maybe the experts will still catch up. - andreymal
    • I understand everything, but catch -4 out of the blue with a well-described question (I answered even more confused ones). The goal of the community is to share knowledge (Founded in 2008, Stack Overflow). Do not know how to tolerate help - go. So I could ask a couple of such questions and catch the autobahn? About "Check for the presence of text at once in all the source code of the page - so-so idea" - I agree, found page_source. - p4sh
    • one
      I saw -4 for this answer, and the minuses were most likely for insults until the moderator erased them. -4 I did not see the question, but if they really were, then I would agree that this is bad and wrong. In the meantime, the spec seems to be pulled up in the next answer :) - andreymal