• 1

  • 1

  • 1

  • from appium import webdriver
    import pytest
    from selenium.webdriver.common.by import By


    class TestXueQiu:

    def setup(self):
    caps = {}
    caps["platformName"] = "android"
    caps["deviceName"] = "127.0.0.1:5037"
    caps["appPackage"] = "com.xueqiu.android"
    caps["appActivity"] = ".view.WelcomeActivityAlias"
    self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
    # 权限获取
    el1 = self.driver.find_element_by_id("com.xueqiu.android:id/tv_open")
    el1.click()
    el2 = self.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button")
    el2.click()
    el3 = self.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button")
    el3.click()


    def test_wrong_phone(self):
    self.driver.find_element(By.ID,"user_profile_icon").click()
    self.driver.find_element(By.ID,'rl_login_more').click()
    self.driver.find_element(By.ID,'login_account').send_keys("177800001")
    self.driver.find_element(By.ID,'login_password').send_keys("123456")
    self.driver.find_element(By.ID,'button_next').click()
    text =self.driver.find_element(By.ID,'md_content').get_attribute("text")
    assert text =="手机号码填写错误"



    def test_wrong_password(self):
    self.driver.find_element(By.ID, "user_profile_icon").click()
    self.driver.find_element(By.ID, 'rl_login_more').click()
    self.driver.find_element(By.ID, 'login_account').send_keys("17780000001")
    self.driver.find_element(By.ID, 'login_password').send_keys("123456")
    self.driver.find_element(By.ID, 'button_next').click()
    text = self.driver.find_element(By.ID, 'md_content').get_attribute("text")
    assert text == "用户名或密码错误"



    @pytest.mark.parametrize("keywords,stockname",[("alibaba","阿里巴巴"),("xiaomi","小米集团-W"),("google","谷歌A")])
    def test_search(self,keywords,stockname):
    self.driver.find_element(By.ID,"tv_search").click()
    self.driver.find_element(By.ID,"search_input_text").send_keys(keywords)
    self.driver.find_element(By.ID,"name").click()
    text=self.driver.find_element(By.ID,"stockName").get_attribute("text")
    assert text==stockname




    if __name__ == '__main__':
    pytest.main()

  • 1

  • 课堂练习

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

    from selenium import webdriver
    import time

    import sys

    from selenium.webdriver.common.by import By

    reload(sys)
    sys.setdefaultencoding('utf-8')


    class testerhome():
    def setup_method(self):
    self.driver = webdriver.Chrome()
    self.driver.get("https://testerhome.com/")

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

    def pagenavigate(self):
    topic = self.driver.find_element(By.CSS_SELECTOR,".title a[title*='先到先得']")
    topic.click()
    time.sleep(2)
    btn =self.driver.find_element(By.CSS_SELECTOR,"button.btn.btn-default")
    time.sleep(2)
    btn.click()
    select =self.driver.find_element(By.XPATH,"//*[@class='list']/li[2]/a")
    time.sleep(2)
    select.click()





    if __name__ == '__main__':
    test =testerhome()
    test.pagenavigate()


    课后作业

    # -*- coding: utf-8 -*-
    import pytest
    import logging
    import allure
    from selenium import webdriver
    from time import sleep

    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys

    logging.basicConfig(level=logging.DEBUG)

    class TestHome:

    def setup_method(self):
    self.driver = webdriver.Chrome()
    self.driver.get("https://testerhome.com/")
    self.driver.implicitly_wait(5)

    # 最新发布的帖子浏览
    @allure.feature('test_newTopic')
    def test_newTopic(self):
    # 点击顶部标题栏-社区
    self.driver.find_element(By.CSS_SELECTOR,'a[href="/topics"]').click()
    # 点击 节点-最新发布
    self.driver.find_element(By.CSS_SELECTOR,'a[href="/topics/last"]').click()
    sleep(2)
    #判断页面标题是否正确
    title = self.driver.title
    if u"最新创建" in title :
    logging.info("页面跳转到最新发布,检查无问题")
    else:
    logging.info("页面跳转有误,请检查相关内容!!!")
    # 获取到第二个帖子的标题
    topic =self.driver.find_element(By.XPATH,'//*[@id="main"]/div/div[1]/div[1]/div[1]/div[2]/div[2]/div[1]/a')
    topic_text = topic.get_attribute('title')
    #选择第二个帖子点击进入
    topic.click()
    sleep(5)
    # 页面title获取
    page_title = self.driver.find_element(By.XPATH,'//*[@id="main-navbar-collapse"]/div[1]/div/a').get_attribute('title')
    if topic_text==page_title:
    logging.info("最新发布帖子浏览,检查完成,无问题")
    else:
    logging.info("页面跳转有误,请检查相关内容!!!!")


    # 断言未登陆访问被拒
    @allure.feature('test_noLoginPermission')
    def test_noLoginPermission(self):
    # 点击顶部标题栏-社团
    self.driver.find_element(By.CSS_SELECTOR, 'a[href="/teams"]').click()
    # 点击霍格沃茨学院
    self.driver.find_element(By.CSS_SELECTOR,'a[title*="霍格沃兹测试学院(hogwarts)"]').click()
    sleep(2)
    self.driver.find_element(By.CSS_SELECTOR,'.title a[href*="/topics/20027"]').click()
    sleep(2)
    tips="访问被拒绝,你可能没有权限或未登录。"
    if tips in self.driver.page_source:
    logging.info("断言未登陆访问被拒,检查完成,无问题")
    else:
    logging.info("页面检查有误,请检查相关内容!!!!")

    #错误的登录方式
    @allure.feature('test_wrongLogin')
    def test_wrongLogin(self):
    # 点击顶部标题栏-登录
    self.driver.find_element(By.CSS_SELECTOR,'a[href="/account/sign_in"]').click()
    #用户名输入框
    user =self.driver.find_element(By.ID,'user_login')
    # 密码输入框
    password =self.driver.find_element(By.ID,'user_password')
    # 登录按钮
    button =self.driver.find_element(By.CSS_SELECTOR,'input[name="commit"]')
    user.clear()
    user.send_keys("testerhome")
    password.clear()
    password.send_keys("123456")
    button.click()
    sleep(2)
    tips="帐号或密码错误。"
    page_tips = self.driver.execute_script("return $('.alert-warning')[0].innerText")
    sleep(2)
    logging.info(page_tips)
    if tips == page_tips:
    logging.info("错误的登录检查完成,无问题")
    else:
    logging.info("页面检查有误,请检查相关内容!!!")

    #搜索”测试媛“,断言标题和搜索的页面标题一致
    @allure.feature('test_topic')
    def test_topic(self):
    # 定位搜索框
    input =self.driver.find_element(By.CSS_SELECTOR,'.form-search div input[name="q"]')
    input.send_keys("测试媛")
    # send_keys模拟键盘enter操作
    input.send_keys(Keys.ENTER)
    sleep(2)
    topic = self.driver.find_element(By.XPATH, '//*[@id="main"]/div/div[2]/div[5]/div[1]/a')
    # js标题获取
    topic_text = self.driver.execute_script("return $('.result div a')[8].text")
    logging.info(topic_text)
    topic.click()
    sleep(2)
    # js页面标题获取
    page_title = self.driver.execute_script("return $('.title')[1].innerText")
    logging.info(page_title)
    if topic_text == page_title:
    logging.info("标题和搜索的页面标题一致,检查完成,无问题")
    else:
    logging.info("页面跳转有误,请检查相关内容!!!!")

    # 销毁并退出
    def teardown_method(self):
    self.driver.quit()


    if __name__ == '__main__':
    # pytest.main()
    pytest.main(['-s', '-q', '--alluredir', './report/xml'])

    # 运行
    # python3 -m pytest seleniumTest.py --alluredir ./report/xml
    # allure generate ./report/xml -o report/html --clean


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

    from selenium import webdriver
    import time

    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')

    class testerhome():
    def __init__(self):
    self.driver = webdriver.Chrome()
    self.driver.get("https://testerhome.com/topics")

    def pagenavigate(self):
    topic = self.driver.find_element_by_xpath('//*[@id="main"]/div/div[1]/div[1]/div[1]/div[3]/div[2]/div[1]/a')
    topic.click()
    time.sleep(2)
    url=self.driver.find_element_by_xpath("//*[@id='main']/div/div[1]/div[1]/div[2]/p[1]/a")
    url.click()
    time.sleep(2)
    text="MTSC2019"
    if text in self.driver.page_source:
    print "------页面验证无问题--------"
    else:
    print "------查找页面内容出错,请核定指定内容是否存在-------"
    time.sleep(2)
    self.driver.quit()

    if __name__ == '__main__':
    test =testerhome()
    test.pagenavigate()

  • 课间作业
    ----conftest.py

    import logging
    import pytest
    logging.basicConfig(level=logging.DEBUG)

    @pytest.fixture(scope="session")
    def session_module():
    logging.info("fury:login")
    yield
    logging.info("fury:logout")

    @pytest.fixture(scope="function")
    def class_module():
    logging.info("fury:class_init")

    ----testfixture.py

    import pytest
    import logging

    import yaml

    logging.basicConfig(level=logging.DEBUG)
    @pytest.mark.usefixtures('session_module')
    class TestA:
    def test_1(self):
    assert [1,2]==[2,1]
    def test_2(self):
    assert True==False
    def test_3(self):
    assert "a" == "b"

    @classmethod
    def teardown_class(cls):
    logging.info("testA quit")

    class TestB:
    # setup = setup_method
    # def setup(self):
    # logging.info("method init")
    @pytest.mark.usefixtures('class_module')
    def test_4(self):
    assert (1,2,3)==(3,2,1)

    @pytest.mark.usefixtures('class_module')
    def test_5(self):
    assert 2==2

    #yaml文件, - 需空一格
    @pytest.mark.usefixtures('class_module')
    @pytest.mark.parametrize("a,b,c",yaml.load(open("pytestData.yaml")))
    def test_6(self,a,b,c):

    assert a+b==c


    class TestC:
    @pytest.mark.run(order=-1)
    def test_7(self):
    assert 2==int("2")

    @pytest.mark.run(order=-3)
    def test_8(self):
    assert 2==2

    @pytest.mark.run(order=-2)
    def test_9(self):
    assert "hello world".find("wor")

    if __name__ == '__main__':
    pytest.main()
  • ----test_pytest.py

    import pytest
    import logging

    import yaml

    logging.basicConfig(level=logging.DEBUG)

    def setup_module():
    logging.info("module login")

    def teardown_module():
    logging.info("module quit")

    class TestA:


    def test_1(self):
    assert [1,2]==[2,1]
    def test_2(self):
    assert True==False
    def test_3(self):
    assert "a" == "b"

    @classmethod
    def teardown_class(cls):
    logging.info("testA quit")

    class TestB:
    # setup = setup_method
    def setup(self):
    logging.info("method init")

    def test_4(self):
    assert (1,2,3)==(3,2,1)

    def test_5(self):
    assert 2==2

    #yaml文件, - 需空一格
    @pytest.mark.parametrize("a,b,c",yaml.load(open("pytestData.yaml")))
    def test_6(self,a,b,c):

    assert a+b==c


    class TestC:
    @pytest.mark.run(order=-1)
    def test_7(self):
    assert 2==int("2")

    @pytest.mark.run(order=-3)
    def test_8(self):
    assert 2==2

    @pytest.mark.run(order=-2)
    def test_9(self):
    assert "hello world".find("wor")

    -----pytestData.yaml

    - [2,1,2]
    - [1,2,0.5]
    - [1000,1,1001]
    - [2,0,null]

    -----运行报告