第七期_selenium page object_20190302

selenium grid

下载最新的selenium server

http_proxy=http://112.126.81.122:$(date +6%m%d) https_proxy=$http_proxy curl -O https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar

从shell服务器上获取

scp [你的用户名]@shell.testing-studio.com:/tmp/selenium-server-standalone-3.141.59.jar  selenium-server-standalone-3.141.59.jar

启动hub

 java -jar selenium-server-standalone-3.141.59.jar -role hub
10:29:43.017 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
10:29:43.198 INFO [GridLauncherV3.lambda$buildLaunchers$5] - Launching Selenium Grid hub on port 4444
2019-03-02 10:29:43.669:INFO::main: Logging initialized @1030ms to org.seleniumhq.jetty9.util.log.StdErrLog
10:29:44.032 INFO [Hub.start] - Selenium Grid hub is up and running
10:29:44.033 INFO [Hub.start] - Nodes should register to http://192.168.0.100:4444/grid/register/
10:29:44.033 INFO [Hub.start] - Clients should connect to http://192.168.0.100:4444/wd/hub

http://127.0.0.1:4444/grid/console

启动node

java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.0.100:4444/grid/register/
10:36:42.857 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
10:36:43.102 INFO [GridLauncherV3.lambda$buildLaunchers$7] - Launching a Selenium Grid node on port 29231
2019-03-02 10:36:43.286:INFO::main: Logging initialized @792ms to org.seleniumhq.jetty9.util.log.StdErrLog
10:36:43.683 INFO [WebDriverServlet.<init>] - Initialising WebDriverServlet
10:36:43.834 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 29231
10:36:43.835 INFO [GridLauncherV3.lambda$buildLaunchers$7] - Selenium Grid node is up and ready to register to the hub
10:36:43.961 INFO [SelfRegisteringRemote$1.run] - Starting auto registration thread. Will try to register every 5000 ms.
10:36:44.403 INFO [SelfRegisteringRemote.registerToHub] - Registering the node to the hub: http://192.168.0.100:4444/grid/register
10:36:44.482 INFO [SelfRegisteringRemote.registerToHub] - The node is registered to the hub and ready to use

以配置文件启动

{
  "capabilities":
  [
    {
      "browserName": "chrome",
      "maxInstances": 3,
      "seleniumProtocol": "WebDriver"
    }
  ],
  "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
  "maxSession": 5,
  "port": 5555,
  "register": true,
  "registerCycle": 5000,
  "hub": "http://localhost:4444",
  "nodeStatusCheckTimeout": 5000,
  "nodePolling": 5000,
  "role": "node",
  "unregisterIfStillDownAfter": 60000,
  "downPollingLimit": 2,
  "debug": false,
  "servlets" : [],
  "withoutServlets": [],
  "custom": {}
}
java -jar selenium-server-standalone-3.141.59.jar -role node -nodeConfig chrome.json
10:42:59.578 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
10:42:59.787 INFO [GridLauncherV3.lambda$buildLaunchers$7] - Launching a Selenium Grid node on port 5555
2019-03-02 10:43:00.025:INFO::main: Logging initialized @838ms to org.seleniumhq.jetty9.util.log.StdErrLog
10:43:00.626 INFO [WebDriverServlet.<init>] - Initialising WebDriverServlet
10:43:00.866 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 5555
10:43:00.866 INFO [GridLauncherV3.lambda$buildLaunchers$7] - Selenium Grid node is up and ready to register to the hub
10:43:00.998 INFO [SelfRegisteringRemote$1.run] - Starting auto registration thread. Will try to register every 5000 ms.
10:43:01.329 INFO [SelfRegisteringRemote.registerToHub] - Registering the node to the hub: http://localhost:4444/grid/register
10:43:01.392 INFO [SelfRegisteringRemote.registerToHub] - The node is registered to the hub and ready to use

hub调用示例代码

import unittest
from selenium import webdriver
from selenium.webdriver import ChromeOptions
import pytest


class TestTesterHome(object):
    def test_testerhome(self):
        options = ChromeOptions()
        options.binary_location = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"

        self.driver = webdriver.Chrome(
            executable_path="/Users/seveniruby/projects/chromedriver/72/chromedriver",
            options=options
        )

        self.driver.get("https://testerhome.com")

    @pytest.mark.parametrize("url",
                             [
                                 "https://testerhome.com",
                                 "https://testing-studio.com",
                                 "https://baidu.com"
                             ]
                             )
    def test_remote(self, url):
        options = webdriver.ChromeOptions()
        options.binary_location = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
        print(options.to_capabilities())
        options.capabilities["platform"] = "MAC"
    
        driver = webdriver.Remote(
            "http://192.168.0.100:4444/wd/hub",
            desired_capabilities=options.to_capabilities(),
        )
        driver.get(url)

Java版本管理

  • brew
  • jenv
  • /usr/libexec/java_home
localhost:~ seveniruby$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
    11.0.2, x86_64:\t"Java SE 11.0.2"\t/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
    10.0.2, x86_64:\t"Java SE 10.0.2"\t/Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
    1.8.0_51, x86_64:\t"Java SE 8"\t/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home

selenium page object

测试用例

  • 搜索
  • 登录
  • 下载
  • 基金入口
  • 新闻入口

pages

from pypom import Page
from selenium.webdriver.common.by import By
from driver.Driver import Driver
from page.Search import Search


class Xueqiu(Page):
    _q=(By.NAME , "q")

    def __init__(self):
        super(Xueqiu, self).__init__(Driver.driver(), "https://xueqiu.com")
        self.open()

    @property
    def loaded(self):
        return self.seed_url in self.driver.current_url

    def search(self, keyword):
        self.find_element(*self._q).send_keys(keyword)
        self.find_element(By.CSS_SELECTOR, ".nav__search").submit()
        return Search()



    def getName(self):
        self.find_element(By.CSS_SELECTOR, ".search__tabs__border > a[href='#/stock']").click()
        text = self.find_element(By.CSS_SELECTOR, ".search__stock__bd__code").text
        return text

from pypom import Page
from selenium.webdriver.common.by import By
from driver.Driver import Driver
from time import sleep

class Search(Page):
    _stock=(By.XPATH, "//*[@class='search__tabs__border']//*[text()='股票']")
    _all=(By.XPATH, "//*[@class='search__tabs__border']//*[text()='综合']")

    def __init__(self):
        super(Search, self).__init__(Driver.driver())

    @property
    def loaded(self):
        self.seed_url="https://xueqiu.com/k"
        return self.seed_url in self.driver.current_url

    def isInStock(self, keyword):
        self.find_element(* self._stock).click()
        x=(By.XPATH, "//table//*[text()='" + keyword + "']")
        return self.is_element_displayed(*x)

测试用例

import pytest
from page.Xueqiu import Xueqiu
from driver.Driver import Driver
import logging

class TestXueqiu(object):
    def setup(self):
        pass

    def test_xueqiu(self):
        xueqiu=Xueqiu()
        search=xueqiu.search("pdd")
        assert True==search.isInStock("拼多多")

    def test_search(self):
        assert True==Xueqiu().search("alibaba").isInStock("阿里巴巴")

    def test_search_error(self):
        assert False==Xueqiu().search("alibaba").isInStock("小米")

    def teardown(self):
        Driver.quit()

数据驱动

报告生成

作业1

编写一个密码找回的自动化测试用例,使用Page Object完成,写完把pages和测试用例的代码贴到回复里