python + selenium 针对calculator测试 (练手)

这次练手的目标:

  1. Open Chrome浏览器.
  2. Launch Website
  3. 计算1 + 1 显示结果。做判断是否正确。
  4. 如失败,截图并保存

主要元素定位技术:
使用xpath定位,绝对定位://*[@id=”sciout”]/tbody/tr[2]/td[2]/div/div[3]/span[1]。
使用try …. except 异常捕获,抛出异常。

# -*- coding:utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from _ast import Pass

calc = webdriver.Chrome()

# Launch website
calc.get("http://www.calculator.net/")
#Maximize the brower
calc.maximize_window()
# The initialization value
one = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[3]/span[1]')
two = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[3]/span[2]')
three = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[3]/span[3]')
add = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[1]/span[4]')
sum1 = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[5]/span[4]')
MR = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[4]/span[5]')
MC = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[5]/span[5]')
resul = calc.find_element_by_id("sciOutPut")

def result(es, res, casename):
    if es == res:
         print  "PASS"
    else:
        print  "FAIL"
        calc.get_screenshot_as_file("F:\calc\\%s.png"  %casename)
        print  "F:\calc\\%s.png"  %casename

# test main
casename = "casename1"
try:
    one.click()
    add.click()
    two.click()
    sum1.click()
    res = resul.text
    time.sleep(2)
    result(3, res, casename)
    MR.click()
    time.sleep(5)
    calc.quit()
except Exception, e:
    print e

你可能感兴趣的:(python,selenium)