python selenium web自动化测试1

1.登录做成通用方法
# -*- coding: utf-8 -*-
from time import sleep

def login_open(driver,base_url,userName,passWord):
    driver.get(base_url + "/index.htm")
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.find_element_by_name("userName").send_keys(userName)
    driver.find_element_by_name("passWord").send_keys(passWord)
    driver.find_element_by_xpath("//button[@type='button']").click()
    sleep(3)
2.测试的页面,创建7种类型的优惠券
image.png
3.具体的操作
from selenium import webdriver
from time import sleep
import random
from a import login_open

# 创建全场代金券
def create_coupon1():
    reduce_Cost = random.uniform(0.1,10)
    reduceCost = str(reduce_Cost)[:4]
    leastCost = str(reduce_Cost + 0.01)[:4]

    driver.get(base_url + "/coupon.htm")
    driver.find_element_by_name("reduceCost").send_keys(str(reduceCost))
    driver.find_element_by_name("leastCost").send_keys(leastCost)
    driver.find_element_by_xpath('//*[@id="saveBtn"]/span[1]').click()
    sleep(3)
    driver.find_element_by_xpath("/html/body/div[8]/div[7]/div/button").click()

# 创建全场满减券
def create_coupon2():
    driver.get(base_url + "/coupon.htm")
    preCost = str(random.uniform(0.1,10))[:4]
    fullReduce = "0.1"

    driver.find_element_by_id("inlineRadio9").click()
    driver.find_element_by_name("preCost").send_keys(preCost)
    driver.find_element_by_name("fullReduce").send_keys(fullReduce)
    driver.find_element_by_xpath('//*[@id="saveBtn"]/span[1]').click()
    sleep(3)
    driver.find_element_by_xpath("/html/body/div[8]/div[7]/div/button").click()

# 创建全场折扣券
def create_coupon3():
    driver.get(base_url + "/coupon.htm")
    discount = "7.7"
    leastCost = "0.1"

    driver.find_element_by_id("inlineRadio1").click()
    driver.find_element_by_name("discount").send_keys(discount)
    driver.find_element_by_name("leastCost").send_keys(leastCost)
    driver.find_element_by_xpath('//*[@id="saveBtn"]/span[1]').click()
    sleep(3)
    driver.find_element_by_xpath("/html/body/div[8]/div[7]/div/button").click()

# 创建礼品兑换券
def create_coupon4():
    driver.get(base_url + "/coupon.htm")
    gift = "礼品兑换" + str(random.uniform(100000,300000))[:6]

    driver.find_element_by_id("inlineRadio5").click()
    driver.find_element_by_name("gift").send_keys(gift)

    driver.find_element_by_xpath('//*[@id="saveBtn"]/span[1]').click()
    sleep(3)
    driver.find_element_by_xpath("/html/body/div[8]/div[7]/div/button").click()

# 创建单品代金券
def create_coupon5():
    driver.get(base_url + "/coupon.htm")
    reduce_Cost = random.uniform(0.1, 10)
    reduceCost = str(reduce_Cost)[:4]
    leastCost = str(reduce_Cost + 0.01)[:4]
    itemNo = str(random.random()*10000000)[:7]
    itemName = 'i'+itemNo
    itemPrice = str(random.uniform(0.1, 10))[:4]

    driver.find_element_by_id("inlineRadio2").click()
    driver.find_element_by_css_selector("[flag='itemNo']").send_keys(itemNo)
    driver.find_element_by_css_selector("[flag='itemName']").send_keys(itemName)
    driver.find_element_by_css_selector("[flag='itemPrice']").send_keys(itemPrice)
    driver.find_element_by_name("reduceCost").send_keys(reduceCost)
    driver.find_element_by_name("leastCost").send_keys(leastCost)
    driver.find_element_by_xpath('//*[@id="saveBtn"]/span[1]').click()
    sleep(3)
    driver.find_element_by_xpath("/html/body/div[8]/div[7]/div/button").click()

# 创建单品特价券
def create_coupon6():
    driver.get(base_url + "/coupon.htm")
    special_price = random.uniform(0.1, 10)
    specialPrice = str(special_price)[:4]
    leastCost = str(special_price + 0.01)[:4]
    itemNo = str(random.random()*10000000)[:7]
    itemName = 'i'+itemNo
    itemPrice = str(random.uniform(0.1, 10))[:4]

    driver.find_element_by_id("inlineRadio8").click()
    driver.find_element_by_css_selector("[flag='itemNo']").send_keys(itemNo)
    driver.find_element_by_css_selector("[flag='itemName']").send_keys(itemName)
    driver.find_element_by_css_selector("[flag='itemPrice']").send_keys(itemPrice)
    driver.find_element_by_name("specialPrice").send_keys(specialPrice)
    driver.find_element_by_name("leastCost").send_keys(leastCost)
    driver.find_element_by_xpath('//*[@id="saveBtn"]/span[1]').click()
    sleep(3)
    driver.find_element_by_xpath("/html/body/div[8]/div[7]/div/button").click()

# 创建单品折扣券
def create_coupon7():
    driver.get(base_url + "/coupon.htm")
    discount = "7.7"
    leastCost = "0.1"
    itemNo = str(random.random()*10000000)[:7]
    itemName = 'i'+itemNo
    itemPrice = str(random.uniform(0.1, 10))[:4]

    driver.find_element_by_id("inlineRadio7").click()
    driver.find_element_by_css_selector("[flag='itemNo']").send_keys(itemNo)
    driver.find_element_by_css_selector("[flag='itemName']").send_keys(itemName)
    driver.find_element_by_css_selector("[flag='itemPrice']").send_keys(itemPrice)
    driver.find_element_by_name("discount").send_keys(discount)
    driver.find_element_by_name("leastCost").send_keys(leastCost)
    driver.find_element_by_xpath('//*[@id="saveBtn"]/span[1]').click()
    sleep(3)
    driver.find_element_by_xpath("/html/body/div[8]/div[7]/div/button").click()

if __name__ == "__main__":
    base_url = "https://xxx.xxxxxxxx.cn"
    userName = "xxxxxx"
    passWord = "111111"
    driver = webdriver.Chrome()
    login_open(driver, base_url, userName, passWord)
    driver.get(base_url + "/xxxxxxxx.htm")
    sleep(1)
    # 每种单独调用
    # create_coupon1()
    # create_coupon2()
    # create_coupon3()
    # create_coupon4()
    # create_coupon5()
    # create_coupon6()
    # create_coupon7()
    # 执行所有类型
    row =  range(1,8)
    for i in row:
        eval("create_coupon"+str(i))()
    print("创建优惠券成功!")

你可能感兴趣的:(python selenium web自动化测试1)