我有一个登录 Salesforce 的 Selenium 脚本,使用标准数据创建联系人并保存。
我想问正在运行的用户使用来自控制台的原始输入创建多少联系人,然后循环脚本 x 次,直到它们被创建。
最好将其添加到下面的代码中吗?或者在终端中运行这个python文件时在控制台中输入一个循环?
谢谢!
注意:我使用了一个介于 0-5000 之间的随机整数来创建一个唯一(足够接近)的姓氏和电子邮件,因此不太可能出现重复。
注意 2:在删除和重复实验之前,我只会创建最多 10 个联系人。
这是代码:
from selenium import webdriver
import unittest
import time
from random import randint
class SalesforceLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get("https://test.salesforce.com")
def test_salesforce_login(self):
driver = self.driver
self.driver.find_element_by_id("username").send_keys("xxxx")
self.driver.find_element_by_id("password").send_keys("xxxx")
driver.find_element_by_id("Login").click()
time.sleep(1)
# Generate random number and assign to lastRnadom
lastRandom = randint(2,5000)
driver.find_element_by_link_text("Contacts").click()
driver.find_element_by_name("new").click()
driver.find_element_by_id("name_firstcon2").clear()
driver.find_element_by_id("name_firstcon2").send_keys("Test")
driver.find_element_by_id("name_lastcon2").clear()
driver.find_element_by_id("name_lastcon2").send_keys(lastRandom)
driver.find_element_by_id("con4").clear()
driver.find_element_by_id("con4").send_keys("xxxx")
driver.find_element_by_id("con15").clear()
# Form an email address from strings and ints
email = ("test" + "@" + str(lastRandom) + ".com")
driver.find_element_by_id("con15").send_keys(email)
driver.find_element_by_id("con10").clear()
driver.find_element_by_id("con10").send_keys("012345678")
driver.find_element_by_id("con12").clear()
driver.find_element_by_id("con12").send_keys("0123456789")
driver.find_element_by_id("con5").clear()
driver.find_element_by_id("con5").send_keys("Mr")
driver.find_element_by_id("con19street").clear()
driver.find_element_by_id("con19street").send_keys("Made Up Mailing Street")
driver.find_element_by_id("con19city").clear()
driver.find_element_by_id("con19city").send_keys("Mailing City")
driver.find_element_by_id("con19state").clear()
driver.find_element_by_id("con19state").send_keys("Mailing State")
driver.find_element_by_id("con19zip").clear()
driver.find_element_by_id("con19zip").send_keys("Mailing Zip")
driver.find_element_by_id("con19country").clear()
driver.find_element_by_id("con19country").send_keys("Mailing Country")
driver.find_element_by_id("con18street").clear()
driver.find_element_by_id("con18street").send_keys("Other Street")
driver.find_element_by_id("con18city").clear()
driver.find_element_by_id("con18city").send_keys("Other City")
driver.find_element_by_id("con18state").clear()
driver.find_element_by_id("con18state").send_keys("Other State")
driver.find_element_by_id("con18zip").clear()
driver.find_element_by_id("con18zip").send_keys("Other Zip")
driver.find_element_by_id("con18country").clear()
driver.find_element_by_id("con18country").send_keys("Other Country")
driver.find_element_by_id("con11").clear()
driver.find_element_by_id("con11").send_keys("Fax")
driver.find_element_by_id("con13").clear()
driver.find_element_by_id("con13").send_keys("Home Phone")
driver.find_element_by_id("con14").clear()
driver.find_element_by_id("con14").send_keys("Other Phone")
driver.find_element_by_id("con16").clear()
driver.find_element_by_id("con16").send_keys("Assistant")
driver.find_element_by_id("con17").clear()
driver.find_element_by_id("con17").send_keys("Asst. Phone")
driver.find_element_by_id("con20").click()
driver.find_element_by_id("con20").clear()
driver.find_element_by_id("con20").send_keys("Description")
driver.find_element_by_name("save").click()
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()