计算机软件测试员(三级)Selenium登录模块测试案例一(Python版)

登录模块测试

使用Selenium IDE来录制登录的测试脚本

Create a new project

开始录制


assert element present确认目标元素存在于页面上的某处。如果断言失败,则测试将停止。

导出Python脚本


使用PyCharm打开编辑

配置测试环境pytest
准备测试数据文件
在该 Python 脚本中,从素材文件夹的用户和密码列表文件 users.csv 导入数据,重复验证登录功能
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


class TestInvalidLogin():
  def setup_method(self, method):
    self.driver = webdriver.Edge()
    self.vars = {}

  def teardown_method(self, method):
    self.driver.quit()

  def test_invalidLogin(self):
    self.driver.get("http://localhost:7272/")
    self.driver.set_window_size(945, 1020)
    filename = "users.csv"
    f = open(filename,'r',encoding="utf-8")
    for line in f :
      
      user_list = line.split(",")
      user_name = user_list[0]
      user_passwd = user_list[1]
      
      self.driver.find_element(By.ID, "username_field").click()
      self.driver.find_element(By.ID, "username_field").send_keys(user_name)
      self.driver.find_element(By.ID, "password_field").click()
      self.driver.find_element(By.ID, "password_field").send_keys(user_passwd)
      # self.driver.find_element(By.ID, "login_button").click()
      
      elements = self.driver.find_elements(By.XPATH, "//h1[contains(.,\'Error Page\')]")
      assert len(elements) > 0

      self.driver.back()
      self.driver.find_element(By.ID, "username_field").clear()
      self.driver.find_element(By.ID, "password_field").clear()

你可能感兴趣的:(计算机软件测试员(三级)Selenium登录模块测试案例一(Python版))