0

I am completely new to development and automation so I've been playing around with Python and Selenium to fill out simple forms. The form I am using will detect existing email addresses in a Database so I need to know how to tell the program to use a unique email address on each automation test start. In this case, the email I want to use is huks214+(Randomvalue)@gmail.com and have that random value change each time I run the script. Below is a code snipit from Selenium

driver.find_element_by_id("Email_Address").send_keys("jhfggf@kjkj.com")
driver.find_element_by_id("Cnfm_Email_Address").click()
driver.find_element_by_id("Cnfm_Email_Address").clear()
driver.find_element_by_id("Cnfm_Email_Address").send_keys("jhfggf@kjkj.com")

This is inputting the email address as I entered it on the form when I recorded it. I have the below code which will change the email address but I'm unsure on how to integrate it into the above script

import string
import random
email = 'huks214+' + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10)) + '@gmail.com'
print (email)
1

Just use the generated value instead of a hardcoded string. Extracting the email generation code into a function would also be a good idea:

import string
import random


def generate_email(prefix='huks214+', domain='gmail.com'):
    random_part = ''.join(random.choice(string.ascii_lowercase + string.digits) 
                          for _ in range(10))

    return prefix + random_part + '@' + domain


# your code that defines driver here
email = generate_email()

driver.find_element_by_id("Email_Address").send_keys(email)
confirm_email = driver.find_element_by_id("Cnfm_Email_Address")
confirm_email.click()
confirm_email.clear()
confirm_email.send_keys(email)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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