1 # -*- coding: utf-8 -*-
2
3 from selenium import webdriver
4 from selenium.webdriver.support.select import Select
5 from selenium.webdriver.common.action_chains import ActionChains
6 import time, SendKeys,os
7 import codecs #用于文件读写,可以指定编码格式
8
9 # 进入ecshop登录界面
10 d = webdriver.Firefox()
11 d.implicitly_wait(10) # 全局等待时间(10秒)
12 d.maximize_window() # 最大化窗口
13 #goods_sn = 'ECS0001102' #货号
14 '''从数据文件中读取测试数据'''
15 fp=codecs.open(os.getcwd()+'\\testData.txt','r',encoding='utf-8') #打开测试数据文档
16 listData = [] #读取测试数据放到list中
17 for item in fp.readlines():
18 listData.append(item.rstrip()) #将取出的数据中换行符去掉
19 goods_name,goods_sn,cat_id,brand_id,suppliers_id=listData[0],listData[1],listData[2],listData[3],listData[4] #取到的值赋值给变量
20 fp.close() #关闭测试数据文档
22 d.get(r'http://192.168.7.85:9000/ecshop3/admin')
23 time.sleep(3)
24 # 登录ecshop
25 d.find_element_by_name('username').clear()
26 d.find_element_by_name('username').send_keys('admin')
27 d.find_element_by_name('password').clear()
28 d.find_element_by_name('password').send_keys('admin123456')
29 d.find_element_by_class_name('btn-a').click()
30 time.sleep(3)
31 # 切换到menu-frame
32 d.switch_to.frame(d.find_element_by_id('menu-frame'))
33 d.find_element_by_link_text('商品列表').click()
34 time.sleep(3)
35
36 d.switch_to.default_content()
37 d.switch_to.frame(d.find_element_by_id('main-frame'))
38 # d.find_element_by_link_text('添加新商品').click()
39 d.find_element_by_css_selector('.action-span.btn-add-goods>a').click()
40 time.sleep(3)
41
42 # 输入商品信息
43 d.find_element_by_name('goods_name').clear()
44 d.find_element_by_name('goods_name').send_keys(goods_name)
45 d.find_element_by_name('goods_sn').clear()
46 d.find_element_by_name('goods_sn').send_keys(goods_sn)
47 Select(d.find_element_by_name('cat_id')).select_by_value(cat_id)
48 Select(d.find_element_by_name('brand_id')).select_by_visible_text(brand_id)
49 Select(d.find_element_by_name('suppliers_id')).select_by_visible_text(suppliers_id)
50 d.find_element_by_name('shop_price').clear()
51 d.find_element_by_name('shop_price').send_keys('5000')
52 d.find_element_by_name('volume_number[]').clear()
53 d.find_element_by_name('volume_number[]').send_keys('2')
54 d.find_element_by_name('volume_price[]').clear()
55 d.find_element_by_name('volume_price[]').send_keys('500')
56 d.find_element_by_name('virtual_sales').clear()
57 d.find_element_by_name('virtual_sales').send_keys('200')
58 d.find_element_by_id('is_promote').click()
59
60 # 运行JS脚本,删除readonly(只读)元素
61 js_start = 'document.getElementById("promote_start_date").removeAttribute("readonly")'
62 d.execute_script(js_start)
63 # 添加日期
64 d.find_element_by_id('promote_start_date').clear()
65 d.find_element_by_id('promote_start_date').send_keys('2018-06-01')
66
67 # js字符串
68 js_end = 'document.getElementById("promote_end_date").removeAttribute("readonly")'
69 # 执行js代码
70 d.execute_script(js_end)
71
72 d.find_element_by_id('promote_end_date').clear()
73 d.find_element_by_id('promote_end_date').send_keys('2018-07-10')
74 # 上传商品图片
75 d.find_element_by_name('goods_img').click() #点击浏览按钮,打开选择文件窗口
76 time.sleep(2)
77 SendKeys.SendKeys('C:\\Users\\Administrator\\Desktop\\123.png')
78 SendKeys.SendKeys('{ENTER}')
79 time.sleep(2)
80
81 d.find_element_by_css_selector('[class=\'button\'][value=\' 确定 \']').click()
82 time.sleep(5)
83 # 添加成功,回到商品列表界面
84 d.find_element_by_link_text('商品列表').click()
85 time.sleep(3)
86 # 获取到自身后用..切换到父辈再到子辈
87 d.find_element_by_xpath('//span[contains(.,"'+goods_sn+'")]/../../td[12]/a[4]').click()
88 time.sleep(2)
89 # 切换到alert
90 js = d.switch_to.alert
91 # 确定
92 js.accept()
文本读写可参考此文