• 作业2

    封装

    用例

  • 1、接口测试多数还是restful这样的多一些吧,dubbo这类的占比有多大?去年面试的时候被问了
    2、各个厂家对API文档是怎么管理的,都有自己的平台么,
    3、偶尔遇到过某个接口有较大变动的时候,比如原来10多个参数减少到4个,结果结构也发生变化,接口自动化用例要怎么设计才能减少工作量

  • Docker 实战 at 2019年03月05日

    练习5:

    子节点

    job

    构建

    工程页

    Allure报告

  • Docker 实战 at 2019年03月03日

    练习4:

  • Docker 实战 at 2019年03月03日

    练习三

    docker run --name=conan -d -p 5000:4200 conan
    docker run --name=holmes -d --net=container:conan holmes

  • Github 地址:https://github.com/GoverSky/selenium_java_demo

    driver ~\src\main\java\driver\Driver.java

    package driver;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import java.util.concurrent.TimeUnit;

    public class Driver {

    public static WebDriver driver;

    public static WebDriver getCurrentDriver(){
    return driver;
    }
    public static void start(){
    driver=new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    }

    public static void stop(){
    driver.quit();
    }

    }

    BasePage ~\src\main\java\page\BasePage.java

    package page;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import driver.Driver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;


    import java.util.List;

    public class BasePage {
    public static BasePage start(){
    Driver.start();
    Driver.getCurrentDriver().get("https://testerhome.com");
    Driver.getCurrentDriver().manage().window().maximize();
    return new BasePage();
    }
    public List<WebElement> findElements(By by){
    return Driver.getCurrentDriver().findElements(by);
    }
    public WebElement findElement(By by){
    WebDriverWait wait= new WebDriverWait(Driver.getCurrentDriver(),5);
    return wait.until(ExpectedConditions.presenceOfElementLocated(by));

    }
    public WebElement findElement(String string){
    By locator= By.xpath("//*[contains(text(),'"+string+"')]");
    WebDriverWait wait= new WebDriverWait(Driver.getCurrentDriver(),5);
    return wait.until(ExpectedConditions.presenceOfElementLocated(locator));

    }

    //切换模块
    public void switch_menu(String string){
    findElement(string).click();
    }
    //获取警告提示
    public String get_waining(){
    return findElement(By.xpath("//*[contains(@class,'alert-danger')]")).getText();
    }
    public void stop(){
    Driver.stop();
    }

    }

    社团页面对象简单封装 ~\src\main\java\page\TeamPage.java

    package page;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;

    import java.util.List;

    public class TeamPage extends BasePage{
    public static TeamPage start(){
    BasePage.start();
    return new TeamPage();
    }

    //选择团队
    public void select_team(String string){
    By locator = By.xpath("//*[@class='team-name' and text()='"+string+"']");
    findElement(locator).click();
    }

    //选择文章
    public void select_topic(Integer index){
    List<WebElement> elements;
    elements = findElements(By.xpath("//*[starts-with(@class,'title')]//a"));
    elements.get(index).click();
    }

    }


    数据驱动封装 ~\src\test\java\Data\TestData.java

    package Data;

    import org.testng.annotations.DataProvider;

    public class TestData {
    @DataProvider
    public Object[][] team_test(){
    return new Object[][] {
    // 模块名称,社团名称,文章索引,预期提示
    {"社团","霍格沃兹测试学院",0,"访问被拒绝,你可能没有权限或未登录。"},
    {"社团","健康部落",0,"访问被拒绝,你可能没有权限或未登录。"},
    };
    }
    }

    测试用例 ~\src\test\java\selenium_Test.java

    import Data.TestData;
    import org.testng.Assert;
    import org.testng.annotations.*;
    import page.TeamPage;

    public class selenium_Test {
    TeamPage teamPage;
    @BeforeTest
    public void start(){
    teamPage = TeamPage.start();
    }

    // 无登录私密社团测试
    @Test(dataProvider ="team_test" ,dataProviderClass = TestData.class)
    public void 社团测试(String menu,String team,Integer index,String expectd){
    teamPage.switch_menu(menu);
    teamPage.select_team(team);
    teamPage.select_topic(index);
    Assert.assertEquals( teamPage.get_waining(),expectd);
    }

    @AfterTest
    public void teardown(){
    teamPage.stop();
    }

    }

  • Github

    https://github.com/GoverSky/XueqiuTesting0113

    代码片段

    Driver配置驱动~\src\main\java\driver\GlobalConfig.java

    package driver;

    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
    import java.io.IOException;

    public class GlobalConfig {
    public AppiumConfig appium;
    static GlobalConfig config;

    public static GlobalConfig load(String path){
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    try {
    config = mapper.readValue(GlobalConfig.class.getResource(path), GlobalConfig.class);
    return config;
    } catch (IOException e) {
    e.printStackTrace();
    return null;
    }
    }
    }

    配置文件~\src\main\resources\data\globalConfig.yaml

    appium:
    app: ""
    url: http://127.0.0.1:4723/wd/hub
    wait: 6
    capabilities:
    platformName: android
    deviceName: dddd
    appPackage: com.xueqiu.android
    appActivity: .view.WelcomeActivityAlias
    autoGrantPermissions: true
    automationName: UiAutomator2

    测试数据配置驱动~\src\main\java\driver\XueqiuConfig.java

    package driver;

    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    public class XueqiuConfig {
    public String username="15600534760";
    public String password="123456";
    static HashMap<String, ArrayList> data=new HashMap<String, ArrayList>();
    public void load(String path){
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    try {
    data = mapper.readValue(XueqiuConfig.class.getResource(path),HashMap.class);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public ArrayList getcase(String casename){
    return data.get(casename);
    }
    }

    测试数据驱动~\src\test\java\TestData\SearchTestData.java

    package TestData;

    import driver.XueqiuConfig;
    import org.junit.jupiter.params.provider.Arguments;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.stream.Stream;

    public class SearchTestData {
    static XueqiuConfig config;
    static ArrayList cases;
    static Stream<Arguments> getcase(String casename) {
    config =new XueqiuConfig();
    config.load("/data/SearchTest.yaml");
    cases = config.getcase(casename);
    int len = cases.size();
    Arguments[] a = new Arguments[len];
    for(int i = 0;i <len;i++){
    String aa =(String) cases.get(i);
    String[] bb = aa.split(",");
    if (bb.length==1){
    a[i]=Arguments.of(bb[0]);
    }else{
    a[i]=Arguments.of(bb[0],bb[1]);
    }
    }
    return Arrays.stream(a);
    }
    static Stream<Arguments> 搜索测试(){
    return getcase("搜索测试");
    }
    static Stream<Arguments> 选择(){
    return getcase("选择");
    }
    static Stream<Arguments> 取消选择(){
    return getcase("取消选择");
    }
    }

    测试数据文件 ~\src\main\resources\data\SearchTest.yaml

    搜索测试:
    - pdd,拼多多
    - alibaba,阿里巴巴
    - sogo,搜狗
    选择:
    - pdd
    - alibaba
    - sogo
    取消选择:
    - pdd
    - alibaba
    - sogo

    测试用例~\src\test\java\SearchTest.java

    import driver.XueqiuConfig;
    import org.junit.jupiter.api.*;
    import org.junit.jupiter.params.ParameterizedTest;
    ;import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
    import org.junit.jupiter.api.Order;
    import org.junit.jupiter.api.TestMethodOrder;

    import org.junit.jupiter.params.provider.MethodSource;
    import page.MainPage;
    import page.SearchPage;

    import java.util.ArrayList;
    import static org.hamcrest.CoreMatchers.equalTo;
    import static org.hamcrest.CoreMatchers.hasItems;
    import static org.hamcrest.MatcherAssert.assertThat;


    @TestMethodOrder(OrderAnnotation.class)
    public class SearchTest {

    static MainPage mainPage;
    static SearchPage searchPage;
    static XueqiuConfig config;

    @BeforeAll
    static void beforeAll(){
    mainPage=MainPage.start();
    searchPage=mainPage.gotoSearch();

    }
    @Order(1)
    @ParameterizedTest
    @MethodSource("TestData.SearchTestData#搜索测试")
    void 搜索测试(String keyword, String name){
    String content=searchPage.search(keyword).getAll().get(0);
    assertThat(content, equalTo(name));
    }
    @Order(2)
    @ParameterizedTest
    @MethodSource("TestData.SearchTestData#选择")
    void 选择(String keyword){
    String toast=searchPage.search(keyword).addSelected();
    assertThat(toast, equalTo("添加成功"));
    System.out.print(toast);
    }

    @Order(3)
    @ParameterizedTest
    @MethodSource("TestData.SearchTestData#取消选择")
    void 取消选择(String keyword){
    ArrayList<String> toast=searchPage.search(keyword).removeSelected();
    assertThat(toast, hasItems("已从自选删除"));
    System.out.print(toast);
    }
    }

  • 写了个,感觉应该能满足

    #-*- coding: utf-8 -*-
    # @Time : 2019/1/25 12:27
    # @File : jsondemo.py
    # @Author : 守望@天空~

    from collections import OrderedDict
    import json
    jsonstr = """
    {"
    item":{"data":{"text":"123"},"children":[{"data":{"text":"234"},"children":[]},{"data":{"text":"345"},"children":[{"data":{"text":"456"},"children":[]},{"data":{"text":"plid"},"children":[{"data":{"text":"567"},"children":[]},{"data":{"text":"678"},"children":[]}]}]}]}}
    """

    def dig_path(jsonstr,prepath=[],paths=[]):
    if not prepath:
    prepath=[]
    t=jsonstr
    if isinstance(t,(dict,OrderedDict)):
    if t:
    for key,value in t.items():
    prepath_tmp=prepath[:]
    prepath_tmp.append(key)
    dig_path(value,prepath_tmp[:],paths)
    else:
    paths.append(prepath)
    elif isinstance(t,list):
    if t:
    for index,value in enumerate(t):
    prepath_tmp=prepath[:]
    prepath_tmp.append(index)
    dig_path(value,prepath_tmp[:],paths)
    else:
    paths.append(prepath)
    else:
    paths.append(prepath)

    if __name__=="__main__":
    paths = []
    ttt = json.loads(jsonstr, object_pairs_hook=OrderedDict)
    dig_path(ttt,paths=paths)
    print("deep","\t","path")
    for i in paths:
    print(len(i),"\t",".".join([str(t) for t in i ]))

  • 作业1,2 github:

    https://github.com/GoverSky/XueqiuTesting0113/tree/v1.0

    作业2:

    ~\src\main\java\page\BasePage.java

    package page;

    import driver.Driver;
    import io.appium.java_client.android.AndroidElement;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import java.util.List;

    public class BasePage {
    static WebElement find(By locator){
    WebDriverWait wait= new WebDriverWait( Driver.getCurrentDriver(),5);
    try{
    return wait.until(ExpectedConditions.presenceOfElementLocated(locator));
    }catch (Exception e){
    if (is_present(text("下次再说"))){
    Driver.getCurrentDriver().findElement(text("下次再说")).click();
    }
    return wait.until(ExpectedConditions.presenceOfElementLocated(locator));
    }

    }
    static List<AndroidElement> finds(By locator){
    try{
    return Driver.getCurrentDriver().findElements(locator);
    }catch (Exception e){
    if (is_present(text("下次再说"))){
    Driver.getCurrentDriver().findElement(text("下次再说")).click();
    }
    return Driver.getCurrentDriver().findElements(locator);
    }
    }

    static String getToast(){
    return find(By.xpath("//*[@class='android.widget.Toast']")).getText();
    }
    static By locate(String locator){
    if(locator.matches("/.*")){
    return By.xpath(locator);
    }else{
    return By.id(locator);
    }
    }
    static By text(String content){
    return By.xpath("//*[@text='"+ content + "']");
    }

    //判断页面中是否存在指定元素
    static Boolean is_present(By locator){
    try {
    Driver.getCurrentDriver().findElement(locator);
    return true;
    }catch (Exception e) {
    return false;
    }
    }
    static String get_toast(){
    return find(By.xpath("//*[@class='android.widget.Toast']")).getText();
    }
    }

    跳转页面对象~\src\main\java\page\MainPage.java

    package page;
    import driver.Driver;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;

    public class MainPage extends BasePage {
    By profile=By.id("user_profile_icon");
    By search = By.id("home_search");
    By myselect = text("自选");

    public static MainPage start(){
    Driver.start();
    return new MainPage();
    }

    public ProfilePage gotoProfile(){
    Driver.getCurrentDriver().findElement(profile).click();
    return new ProfilePage();
    }

    public SearchPage gotoSearch(){
    find(search).click();
    return new SearchPage();
    }

    public MySelectPage gotoMySelect(){
    WebElement element = find(myselect);
    WebElement ele;
    int n=4;
    while(n>0){
    ele = find(myselect);
    if (ele.getLocation().toString().equals(element.getLocation().toString())){
    ele.click();
    break;
    }else{
    n++;
    element=ele;
    }
    }
    return new MySelectPage();
    }
    }

    自选页面对象 ~\src\main\java\page\MySelectPage.java

    package page;

    import driver.Driver;
    import io.appium.java_client.TouchAction;
    import io.appium.java_client.android.AndroidElement;
    import io.appium.java_client.touch.offset.PointOption;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;

    import java.util.ArrayList;

    public class MySelectPage extends BasePage {

    //股票标签
    public MySelectPage 股票(){
    find(By.id("com.xueqiu.android:id/page_type_stock")).click();
    return this;
    }
    //基金标签
    public MySelectPage 基金(){
    find(By.id("com.xueqiu.android:id/page_type_fund")).click();
    return this;
    }
    // 组合标签
    public MySelectPage 组合(){
    find(By.id("com.xueqiu.android:id/page_type_cube")).click();
    return this;
    }

    //切换字标签
    public MySelectPage Switch_tab(String locator){
    find(text(locator)).click();
    return this;
    }
    //从搜索中添加
    public String addFromSearch(String keyword){
    find(By.xpath("//*[contains(@text,'添加其他')]")).click();
    SearchPage searchPage= new SearchPage();
    searchPage.search(keyword).addSelected();
    String Toast = get_toast();
    find(By.id("action_close")).click();
    // 处理提示框
    if (is_present(By.id("iv_close"))){
    find(By.id("iv_close")).click();
    }
    return Toast;
    }

    // 从搜索中移除
    public String removeFromSearch(String keyword){
    find(By.id("action_create_cube")).click();
    SearchPage searchPage= new SearchPage();
    searchPage.search(keyword).removeSelected();
    String Toast = get_toast();
    find(By.id("action_close")).click();
    return Toast;
    }

    //从热门股票添加
    public ArrayList<String> addFromRecommend(){
    find(By.id("add_to_portfolio_stock")).click();
    ArrayList<String> array=new ArrayList<String>();
    for(WebElement e: finds(By.id("portfolio_stockName"))){
    array.add(e.getText());
    }
    return array;
    }

    // 长按移除
    public ArrayList<String> removeFromTap(){
    ArrayList<String> array=new ArrayList<String>();

    for(AndroidElement e: finds(By.id("portfolio_stockName"))){
    TouchAction t= new TouchAction(Driver.getCurrentDriver());
    PointOption p = new PointOption();
    p.withCoordinates(e.getLocation().getX(),e.getLocation().getY());
    t.longPress(p).release().perform();
    find(By.xpath("//*[contains(@text,'删除')]")).click();
    // 取属性问题 https://blog.csdn.net/bear_w/article/details/50330753
    array.add(get_toast());
    }
    return array;
    }
    }

    自选测试用例~ ~\src\test\java\MySelectTest.java

    import org.junit.jupiter.api.*;
    import org.junit.jupiter.params.ParameterizedTest;
    import org.junit.jupiter.params.provider.CsvSource;
    import page.*;

    import java.util.ArrayList;

    import static org.hamcrest.CoreMatchers.equalTo;
    import static org.hamcrest.CoreMatchers.hasItems;
    import static org.hamcrest.MatcherAssert.assertThat;

    @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
    public class MySelectTest {

    static MainPage mainPage;
    static MySelectPage mySelectPage;

    @BeforeAll
    static void beforeAll(){
    mainPage=MainPage.start();
    mySelectPage=mainPage.gotoMySelect();
    }

    @Order(1)
    @Test
    void 从热门中添加(){
    mySelectPage.基金();
    mySelectPage.Switch_tab("基金");
    ArrayList<String> array = mySelectPage.addFromRecommend();
    assertThat(array.size(),equalTo(6));

    }

    @Order(2)
    @Test
    void 从列表中移除(){
    mySelectPage.基金();
    mySelectPage.Switch_tab("基金");
    ArrayList<String> array = mySelectPage.removeFromTap();
    assertThat(array,hasItems("已从自选删除"));

    }

    @Order(3)
    @ParameterizedTest
    @CsvSource({
    "pdd",
    "alibaba",
    "sogo"
    })
    void 从所搜索中添加(String keyword){
    mySelectPage.股票();
    mySelectPage.Switch_tab("沪深");
    String toast= mySelectPage.addFromSearch(keyword);;
    assertThat(toast, equalTo("添加成功"));
    }
    @Order(4)
    @ParameterizedTest
    @CsvSource({
    "pdd",
    "alibaba",
    "sogo"
    })
    void 从所搜索中移除(String keyword){
    mySelectPage.股票();
    mySelectPage.Switch_tab("沪深");
    String toast= mySelectPage.removeFromSearch(keyword);;
    assertThat(toast, equalTo("已从自选删除"));
    }
    }

  • netstat|grep ":ssh"|awk '{print $5}'|uniq -c|wc -l

笨鸟先飞