>[info]根据前面章节的学习,这里开始编写自己的第一个自动化用例 > ## 实战要求: 用例一: * 打开移动端网站 `http://a.4399en.com/` * 登录,账号/密码为是: `gfc@qq.com`/`123456` * 登录后,断言是否已经处于登录状态 * 关闭浏览器 用例二: * 打开移动端网站 `http://a.4399en.com/` * 登录,账号/密码为是: `gfc@123.com`/`123456` * 断言是否弹窗提示“User does not exist!” * 关闭弹窗 * 关闭浏览器 ## 或许,有些朋友会写成像以下这样 用例一,编写脚本 `login_success.py` ```python #!/usr/bin/python # coding=utf-8 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time # 创建Chrome驱动实例,这里创建driver时,传入chrome_options参数,告诉服务器,我是用移动端浏览器访问的。 options = webdriver.ChromeOptions() options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30') driver = webdriver.Chrome(chrome_options=options) # 设置浏览器大小,让它看起来跟手机的样式差不多。 driver.set_window_size("380", "680") # 设置一个全局的等待超时时间 10s driver.implicitly_wait(10) # 打开首页 driver.get("http://a.4399en.com/") # 点击右上角登录按钮打开登录窗口 driver.find_element_by_class_name("CNlogin").click() # 在用户名输入框中输入正确的用户名 wait = WebDriverWait(driver,20,0.5) account = wait.until(EC.visibility_of_element_located((By.ID,"modify-account"))) # account = driver.find_element_by_id("modify-account") account.clear() account.send_keys("gfc@qq.com") # 在密码输入框中输入正确的密码 password = driver.find_element_by_id("modify-password") password.clear() password.send_keys("123456") # 点击登录按钮 driver.find_element_by_xpath("//input[@type='submit'][@value='Login']").click() # 休息3s time.sleep(3) # 断言页面已经登录,已经登录的页面中,会显示用户账号信息。 assert "gfc@qq.com" in driver.page_source # 关闭浏览器 driver.quit() ``` 用例二,编写脚本 `login_fail.py`: ```python #!/usr/bin/python # coding=utf-8 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time # 创建Chrome驱动实例,这里创建driver时,传入chrome_options参数,告诉服务器,我是用移动端浏览器访问的。 options = webdriver.ChromeOptions() options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30') driver = webdriver.Chrome(chrome_options=options) # 设置浏览器大小,让它看起来跟手机的样式差不多。 driver.set_window_size("380", "680") # 设置一个全局的等待超时时间 10s driver.implicitly_wait(10) # 打开首页 driver.get("http://a.4399en.com/") # 点击右上角登录按钮打开登录窗口 driver.find_element_by_class_name("CNlogin").click() # 在用户名输入框中输入正确的用户名 wait = WebDriverWait(driver, 20, 0.5) account = wait.until(EC.visibility_of_element_located((By.ID, "modify-account"))) # account = driver.find_element_by_id("modify-account") account.clear() account.send_keys("gfc@123.com") # 在密码输入框中输入正确的密码 password = driver.find_element_by_id("modify-password") password.clear() password.send_keys("123456") # 点击登录按钮 driver.find_element_by_xpath("//input[@type='submit'][@value='Login']").click() # 休息3s time.sleep(3) # 切换到弹窗窗口 alert = driver.switch_to.alert # 断言页面是否有弹窗,弹窗信息为"User does not exist!" assert "User does not exist!" in alert.text # 确定弹窗 alert.accept() # 关闭浏览器 driver.quit() ``` 然后分别去运行`login_success.py` 和 `login_fail.py`。 ## 你有没有觉得,这样子很挫? * 代码中写死了测试数据,如何换个账号,要修改用例代码? * 代码中写死了页面元素定位信息,如果多个位置定位相同的元素,一旦元素定位信息发生改变,是不是需要到处去修改? * 没有用例组织,一个用例一个文件,很多代码冗余。 * 缺少用例报告。 如果你也意识到这些问题,那么请继续往下看,后面的章节会告诉你如何去优化这个实战的代码。 <hr style="margin-top:50px"> <section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: 40px 0% 10px;box-sizing: border-box;"><section class="" style="display: inline-block;width: 100%;border-width: 5px;border-style: double;border-color: rgb(23, 22, 24);padding: 10px;border-radius: 2px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(20px, 0px, 0px);-webkit-transform: translate3d(20px, 0px, 0px);-moz-transform: translate3d(20px, 0px, 0px);-o-transform: translate3d(20px, 0px, 0px);font-size: 11px;margin: -50px 0% 0px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;width: 7em;height: 7em;display: inline-block;vertical-align: bottom;border-radius: 100%;border-width: 4px;border-style: double;border-color: rgb(23, 22, 24);background-position: center center;background-repeat: no-repeat;background-size: cover;background-image: url(&quot;http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n&quot;);"><section class="" style="width: 100%;height: 100%;overflow: hidden;box-sizing: border-box;"><img class="" data-ratio="0.6012024" data-w="499" data-src="http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n" style="opacity: 0; box-sizing: border-box; width: 100% !important; height: auto !important; visibility: visible !important;" width="100%" data-type="jpeg" _width="100%" src="http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n" data-fail="0"></section></section></section></section><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: -30px 0% 30px;box-sizing: border-box;"><section class="" style="display: inline-block;vertical-align: top;width: 61.8%;padding: 0px 15px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: 40px 0% 0px;box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">微信公众号:</p><p style="margin: 0px;padding: 0px;box-sizing: border-box;">python测试开发圈</p><p style="margin: 0px;padding: 0px;box-sizing: border-box;"><br style="box-sizing: border-box;"></p></section></section></section></section><section class="" style="display: inline-block;vertical-align: top;width: 38.2%;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="text-align: center;margin: 10px 0% 0px;box-sizing: border-box;"><section class="" style="max-width: 100%;vertical-align: middle;display: inline-block;border-width: 0px;border-radius: 0px;box-shadow: rgb(0, 0, 0) 0px 0px 0px;width: 90%;overflow: hidden !important;box-sizing: border-box;"><img data-ratio="1" data-w="430" data-src="http://pav7h2emv.bkt.clouddn.com/FibGgIJSMfHtehzeWOOzjdQKSMx5" style="vertical-align: middle; max-width: 100%; box-sizing: border-box; width: 100% !important; height: auto !important; visibility: visible !important;" width="100%" data-type="jpeg" _width="100%" class="" src="http://pav7h2emv.bkt.clouddn.com/FibGgIJSMfHtehzeWOOzjdQKSMx5" data-fail="0"></section></section></section></section></section></section><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: -30px 0% 0px;box-sizing: border-box;"><section class="" style="display: inline-block;vertical-align: top;width: 61.8%;padding: 0px 15px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(5px, 0px, 0px);-webkit-transform: translate3d(5px, 0px, 0px);-moz-transform: translate3d(5px, 0px, 0px);-o-transform: translate3d(5px, 0px, 0px);box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);font-size: 14px;box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">一起分享学习与成长路线</p></section></section></section></section><section class="" style="display: inline-block;vertical-align: top;width: 38.2%;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(10px, 0px, 0px);-webkit-transform: translate3d(10px, 0px, 0px);-moz-transform: translate3d(10px, 0px, 0px);-o-transform: translate3d(10px, 0px, 0px);box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);font-size: 14px;box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">长按(或扫描)二维码关注</p></section></section></section></section></section></section></section></section></section>