How to make while loop for example 5 automatic registration account this is my code and I want to make an automatic python script for 5 registration account

from selenium import webdriver

driver = webdriver.Chrome("C:\chromedriver_win32\chromedriver.exe")
driver.set_page_load_timeout(30)

driver.get("http://site.com")
driver.maximize_window()
driver.implicitly_wait(20)
driver.find_element_by_xpath("(//a[contains(text(),'Register')])[2]").click()
driver.find_element_by_id('first_name').send_keys("joe");
driver.find_element_by_id('last_name').send_keys("joe");
driver.find_element_by_css_selector('div.page-columnMain > form > div.form-group > #email').send_keys("test@test.com")
driver.find_element_by_css_selector('div.form-group.form-group--half > #password').send_keys("test2");
driver.find_element_by_id('password_confirmation').send_keys('test2');
driver.find_element_by_name("agree").click();
driver.find_element_by_xpath("(//button[@type='submit'])[2]").click();
  • for example i want username:test1,test2,test3,test4 password:test234 and for 4 different mails in my example test1@test.com test2@test.com test3@test.com... – breakfast_club Jul 7 at 13:59
up vote 0 down vote accepted

Create a method to perform the registration operation just like in the code below. Store the test data as you have mentioned in the comment above in some data-structure or file.

fanme | lname | email | username | password

fanme | lname | email | username | password

Call the below method for each test data.

private void perform_registration(Registration _reg) {
// your webdriver interaction goes here -- something like this
_fname.sendKeys(_reg.getFName()); 
_lname.sendkeys(_reg.getLName)
_email.sendKeys(_reg.getEmail()); 
    // other operation continues
}

This nothing to do with Selenium, this is plain simple language construct; how you organize your code, so that you get properly re-usable code which is easy to maintain.

  • I made like you show me txt file , and save like data.txt how to put in my program for my example up stare? i have in txt fname, lname, email, pass, repeat pass ? – breakfast_club Jul 8 at 19:10
  • fname|lname|email|pass how to make this prog with while loop? – breakfast_club Jul 8 at 19:12
  • Just an observation: The underscore on the argument and the getX methods names are not pythonic. Simply reg and get_file / get_name / get_email is the way. – João Farias Jul 8 at 20:55
  • First you have to read the external txt file and then retrieve the values separating by '/'. Now what you have is your test data in some python object. Pass them to your tests by a data provider(pytest or unittest has this mechanism - data driven testing). – Kshetra Mohan Prusty Jul 9 at 5:10
  • The fname,lname etc are attributes of Registration object if you follow the above example. So you will be passing that object to your test method. Now you have the control how many number of times you want to run that method. – Kshetra Mohan Prusty Jul 9 at 5:13

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.