Web端(微信小程序)H5+java+selenium自动化

前置步骤,创建工程(可以testng或普通工程),这里默认是创建的testng工程,前面文章中有讲如何配置好testng工程

当前很多微信小程序都是H5实现,这章主要是讲如何测试H5自动化。

直接上代码

首先:编写了一个公共在网页端打开H5页面的类

package Public;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;
import data.PubValue;

import page.Login_PO;

public class PublicFuction {

    public WebDriver my_dr;
    public WebDriverWait wait;
    PubValue pv = new PubValue();//可以注释掉, PubValue类中主要放了预制参数,chromDriver路径等等



    // H5自动化启动
    public void mobile(WebDriver my_dr, WebDriverWait wait, String URL) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", pv.chromeDriver);//--pv.chromeDriver 的启动路径,这里可以写死
        Map<String, String> mobileEmulation = new HashMap<String, String>();
        // 设置设备,例如:Google Nexus 7/Apple iPhone 6
        // mobileEmulation.put("deviceName", "Google Nexus 7");
        // mobileEmulation.put("deviceName", "iPhone 6 Plus");
        // //这里是要使用的模拟器名称,就是浏览器中模拟器中的顶部型号
        mobileEmulation.put("deviceName", "iPhone X");

        Map<String, Object> chromeOptions = new HashMap<String, Object>();
        chromeOptions.put("mobileEmulation", mobileEmulation);

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
        try {
            System.out.println("开始启动driver~~~");
            my_dr = new ChromeDriver(capabilities);
            wait = new WebDriverWait(my_dr, 10);
            my_dr.get(URL);
            System.out.println("启动driver成功~~~");
        } catch (Exception e) {
            System.out.println("启动driver失败~~~");
            System.out.println(e.getMessage());
        }

        this.my_dr = my_dr;
        this.wait = wait;
    }

}

编写用例,调用写好的公共类

package testCase.H5;

import org.testng.annotations.Test;

import Public.PublicFuction;
import Public.SqlFuction;
import data.PubValue;
import page.H5.AddArchivesSurvey_PO;
import util.ExcelUtil;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
/**
 *
 */
public class AddArchiversSurvey {

    PubValue pv = new PubValue();
    PublicFuction pc = new PublicFuction();
    SqlFuction sf = new SqlFuction();
    AddArchivesSurvey_PO as = new AddArchivesSurvey_PO();//定位页面元素的类,当然初学者也可以注释掉,如果

  
    @Test(description="启动类")     
    public void testAddArchiversSurvey() throws Exception {

        // 启动H5页面
        pc.mobile(pc.my_dr, pc.wait, pv.archivesSurveyURL);//pv.archivesSurveyURL 访问的H5的页面
        Thread.sleep(5000);
        WebDriver my_dr = pc.my_dr;
        WebDriverWait wait = pc.wait;

       //打开页面,后续就可以开展H5自动化,H5页面的自动化跟web端一样

        Thread.sleep(2000);
        my_dr.quit();
    }

}

这个是一个简单的教你如果使用java+selenium就能构建到H5页面的自动化,具体操作步骤,跟普通的web前端一样
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值