在现代软件开发过程中,自动化测试是提高效率、减少重复劳动的重要手段。自动化测试工具能够帮助开发者模拟用户操作,自动执行测试任务,从而节省大量时间。本文将介绍一个基于Selenium的自动化网页测试工具的实现过程,包括技术选型、环境配置、代码实现等。
在众多自动化测试工具中,Selenium因其强大的功能和广泛的社区支持而广受欢迎。Selenium是一个用于自动化Web应用程序测试的工具,它可以模拟用户在浏览器中的操作,实现自动化测试。此外,还使用了Python语言进行脚本编写,以及xlwt库来处理Excel数据。
为了使用Selenium进行自动化测试,首先需要配置好开发环境。以下是环境配置的步骤:
$ brew install geckodriver
安装完成后,需要将geckodriver放置在/usr/local/bin/目录下,或者在代码中指定其路径。
$ pip3 install -r requirements.txt
最后,通过以下命令运行测试脚本:
$ python3 app.py
代码实现主要包括以下几个部分:
在初始化部分,导入了selenium、time、xlwt等模块,并创建了Firefox的WebDriver对象。为了节省资源,启用了无头模式。以下是初始化部分的代码:
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
import xlwt
options = webdriver.FirefoxOptions()
options.add_argument('-headless')
driver = webdriver.Firefox(firefox_options=options)
workbook = xlwt.Workbook()
导航函数用于打开浏览器,导航到指定的网页,并输入查询条件。以下是导航函数的代码:
def navigation():
driver.get("https://www.pakwheels.com")
WebDriverWait(driver, 500).until(EC.element_to_be_clickable((By.ID, 'onesignal-popover-cancel-button')))
driver.find_element_by_id('onesignal-popover-cancel-button').click()
time.sleep(5)
driver.find_element_by_link_text('Used Cars').click()
driver.find_element_by_id('more_option').click()
driver.find_element_by_name('home-query').send_keys('Honda Civic')
driver.find_element_by_class_name('chzn-single').click()
driver.find_element_by_id('UsedCity_chzn_o_4').click()
pr_range = driver.find_element_by_id('pr-range-filter')
pr_range.click()
driver.find_element_by_id('pr_from').send_keys('10')
driver.find_element_by_id('pr_to').send_keys('18')
pr_range.click()
yr_from = Select(driver.find_element_by_id('YearFrom'))
yr_to = Select(driver.find_element_by_id('YearTo'))
yr_from.select_by_value('2008')
yr_to.select_by_value('2012')
driver.find_element_by_id('used-cars-search-btn').click()
get_car_links()
获取链接函数用于获取符合条件的广告链接。以下是获取链接函数的代码:
def get_car_links():
navigation()
links = []
elems = driver.find_elements(By.XPATH, '//a[@class = "car-name ad-detail-path"]')
for elem in elems:
links.append(elem.get_attribute('href'))
return links
def scrape_output(links):
style = xlwt.easyxf('font: bold 1')
sheet1 = workbook.add_sheet('Report')
sheet1.write(0, 0, 'Phone Number', style)
sheet1.write(0, 1, 'Milage', style)
sheet1.write(0, 2, 'Year', style)
sheet1.write(0, 3, 'Transmission', style)
sheet1.write(0, 4, 'Price', style)
sheet1.write(0, 5, 'Engine Capacity', style)
sheet1.write(0, 6, 'Color', style)
sheet1.write(0, 7, 'Registration', style)
sheet1.write(0, 8, 'Link', style)
row = 1
for link in links:
driver.get(link)
driver.find_element(By.XPATH, '//button[@class="btn btn-large btn-block btn-success phone_number_btn"]').click()
phone_number = driver.find_element(By.XPATH, '//*[@id="scrollToFixed"]/div[2]/div[1]/button[1]/span').text
milage = driver.find_element(By.XPATH, '//*[@id="scroll_car_info"]/table/tbody/tr/td[2]/p').text
car_year = driver.find_element(By.XPATH, '/html/body/div[2]/section[2]/div/div[2]/div[1]/div/table/tbody/tr/td[1]/p').text
transmission = driver.find_element(By.XPATH, '//*[@id="scroll_car_info"]/table/tbody/tr/td[4]/p').text
price = driver.find_element(By.XPATH, '//*[@id="scrollToFixed"]/div[2]/div[1]/div/strong').text
engine_capacity = driver.find_element(By.CSS_SELECTOR, '#scroll_car_detail > li:nth-child(8)').text
color = driver.find_element(By.CSS_SELECTOR, '#scroll_car_detail > li:nth-child(4)').text
registration = driver.find_element(By.CSS_SELECTOR, '#scroll_car_detail > li:nth-child(2)').text
sheet1.write(row, 0, phone_number)
sheet1.write(row, 1, milage)
sheet1.write(row, 2, car_year)
sheet1.write(row, 3, transmission)
sheet1.write(row, 4, price)
sheet1.write(row, 5, engine_capacity)
sheet1.write(row, 6, color)
sheet1.write(row, 7, registration)
sheet1.write(row, 8, link)
row += 1
workbook.save('output.xls')