线上七期_Appium 设计模式与数据驱动_20181027

1.用例编写回顾

public class NewDemo {


        @AndroidFindBy(id = "com.xueqiu.android:id/user_profile_icon")
        public MobileElement myCenter;

        @AndroidFindBy(id = "com.xueqiu.android:id/tv_login")
        public AndroidElement login;

        @AndroidFindBy(xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.ImageView")
        public AndroidElement huzhi;

        AppiumDriver driver = null;
        @Before
        public void setUp(){
                DesiredCapabilities capabilities = new DesiredCapabilities();

                capabilities.setCapability("platformName",
                        "Android");
//        capabilities.setCapability("platformVersion",
//                "6.0");
                capabilities.setCapability("deviceName",
                        "P4M0215713015683"); //192.168.56.101:5555
                capabilities.setCapability("app",
                        "com.xueqiu.android");
                capabilities.setCapability("appPackage",
                        "com.xueqiu.android");
                capabilities.setCapability("appActivity",
                        ".view.WelcomeActivityAlia");  //.MainActivity
                                   driver = new AndroidDriver(new URL(
                                "http://127.0.0.1:4723/wd/hub"), capabilities);
                } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                driver.manage().timeouts()
                        .implicitlyWait(15, TimeUnit.SECONDS);

                //超时时间
                final int TIMEOUT = 10;
                TimeOutDuration timeOutDuration = new TimeOutDuration(10, TimeUnit.SECONDS);

                PageFactory.initElements(new AppiumFieldDecorator(driver,timeOutDuration) , this);
        }

        @Test
        public void testCase(){
                myCenter.click();
                login.click();
        }
}
  1. 驱动工厂
package Driver;

import Util.SystemHelper;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class DriverFactory {

    public static AppiumDriver driver;

    /**
     * 生成androidDriver
     */
    public static AppiumDriver getAndroidDriver(){

        DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.setCapability("platformName",
                "Android");
//        c "6.0");
        capabilities.setCapability("deviceName",
                "192.168.56.101:5555"); ////192.168.56.101:5555 , P4M0215713015683
        capabilities.setCapability("appPackage",
                "com.xueqiu.android");
        capabilities.setCapability("appActivity",
                ".view.WelcomeActivityAlias");  //.MainActivity
      capabilities.setCapability("noReset",true);
        capabilities.setCapability("unicodeKeyboard",true);
        capabilities.setCapability("resetKeyboard",true);
        try {
            //初始化驱动
            driver = new AndroidDriver(new URL(
                    "http://127.0.0.1:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        driver.manage().timeouts()
                .implicitlyWait(15, TimeUnit.SECONDS);
        
        return driver;
    }
}
  1. BasePage
public class BasePage {

    public static AppiumDriver driver = DriverFactory.getAndroidDriver();

    //超时时间
    private final int TIMEOUT = 10;
    TimeOutDuration timeOutDuration = new TimeOutDuration(10, TimeUnit.SECONDS);

    public BasePage(){
        //初始化所有元素
        System.out.println("initEelments ");
        PageFactory.initElements(new AppiumFieldDecorator(driver,timeOutDuration) , this);
        System.out.println(this.getClass() + " init sucess");
    }
}
  1. MainPage
public class MainPage extends BasePage {


    @AndroidFindBy(xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TabHost/android.widget.LinearLayout/android.widget.TabWidget/android.widget.RelativeLayout[2]/android.widget.ImageView")
    public AndroidElement zixuan;

    @AndroidFindBy(xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TabHost/android.widget.LinearLayout/android.widget.TabWidget/android.widget.RelativeLayout[1]/android.widget.ImageView")
    public AndroidElement xueqiu;

}
  1. 数据驱动CASE
@RunWith(Parameterized.class)
public class XueqiuCase {

    MainPage mainPage = new MainPage();
    ZiXuanPage ziXuanPage = new ZiXuanPage();
    DetailsPage detailsPage = new DetailsPage();

    /****
     * 数据驱动
     */
    public String stockName;
    public String count;

    public XueqiuCase(String stockName,String count){
        this.stockName = stockName;
        this.count = count;
    }

    @Parameterized.Parameters()
    public static Collection<String []> getParams(){
        return Arrays.asList(new String [][]{
                {"中国平安","1"},
                {"华夏银行","2"},
                {"贵州茅台","3"}
        });
    }
    /*** end ***/


    @Test
    public void addGuanzhu(){

        mainPage.zixuan.click();
        ziXuanPage.searchButton.click();
        ziXuanPage.searchText.sendKeys(stockName);
        if(ziXuanPage.gupiaoList.size() > 0) {
            ziXuanPage.gupiaoList.get(0).click();
        }else{
            ziXuanPage.searchText.sendKeys("中国平安"); //万一关键字搜索无结果,保底方案
            ziXuanPage.gupiaoList.get(0).click();
        }
        System.out.println(detailsPage.sheZixuan.size());
        for(MobileElement mobileElement : detailsPage.sheZixuan){

            if(mobileElement.getText().equals("加自选")){
                mobileElement.click();
            }
        }
        detailsPage.backButton.click();
        ziXuanPage.quxiao.click();
        Assert.assertEquals(ziXuanPage.stockNames.size(),Integer.parseInt(count));
    }

课后作业1:
利用数据驱动的方式,在雪球app上,添加3支股票关注,并在用例最后判断是否添加成功(数量一致)。
首页 --> 自选 --> 搜索 --> 输入搜索关键字(数据驱动) --> 详情页 --> 加自选 --> 验证已加自选数量(数据驱动)。
并且可以实现持续复用。(需要考虑数据回收)

关闭