关键字驱动框架入门教程-6-建立常数变量

      到目前为止,我们在java代码中使用了很多硬编码,例如百度浏览器地址和每个元素定位表达式和用户名和密码字段。有时候,硬编码是不可取的,例如用户名和密码可能在多个地方需要使用,在代码中,我们建议把一些常用的变量给提取出来,放到单独类或者配置文件中去,方便统一管理和修改。这样,如果出现变化,你只需要改动一处,其他地方就不用管。这篇,我们介绍把这些提取出来的常量利用Java中的常数变量来保存,就好像系统里的环境变量一样。

 

建立Java常量

 

1)右键包config,然后选择新建一个class,名称为Constants.java

2)然后给这些常量命名,进行赋值操作。

 

具体Constants.java内容如下

package config;

public class Constants {

    // 这里定义为public static的类型,方便其他任何类进行访问和调用
    public static final String URL = "https://www.baidu.com";
    public static final String Path_TestData = ".//src//dataEngine//dataEngine.xlsx";
    public static final String File_TestData = "dataEngine.xlsx";

    // dataEngine.xlsx中一些单元格的索引值
    public static final int Col_TestCaseID = 0;
    public static final int Col_TestScenarioID =1 ;
    public static final int Col_ActionKeyword =3 ;

    // DataEngmine.excel中sheet的名称
    public static final String Sheet_TestSteps = "Test Steps";

    // 测试登录用到的用户数据
    public static final String UserName = "xxxxxx";
    public static final String Password = "xxxxxxx";
}

3)接下来,我们需要调整下ActionsKeywords.java.

package config;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

public class ActionsKeywords {

    public static WebDriver driver;

    /**
     * 以下方法,我们针对dataEngine.xlsx文件中的action_keyword这列的关键字都进行封装
     * 等关键字框架快设计完了,我们再来调整,读取配置文件去启动不同测试浏览器和测试地址
     * 这样就不会代码写死这两个参数。
     */
    public static void openBrowser() {

        // 这里,我们暂时都写死用chrome来进行自动化测试
        System.setProperty("webdriver.chrome.driver",".\\libs\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    public static void openUrl() {
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.get(Constants.URL);

    }

    public static void click_Login_link() {
        driver.findElement(By.xpath(".//*[@id='u1']/a[text()='登录']")).click();
        driver.findElement(By.id("TANGRAM__PSP_10__footerULoginBtn")).click();
    }

    public static void input_Username() {
        driver.findElement(By.id("TANGRAM__PSP_10__userName")).sendKeys(Constants.UserName);
    }

    public static void input_Password() {
        driver.findElement(By.id("TANGRAM__PSP_10__password")).sendKeys(Constants.Password);
    }

    public static void click_Submit() {
        driver.findElement(By.id("TANGRAM__PSP_10__submit")).click();
    }

    // 关闭浏览器并退出
    public static void closeBrowser() {
        driver.quit();
    }
}


       其实上面就改了url, username,password三处地方的硬编码,关于元素表达式后面有其他文章介绍如何建立元素仓库文件。

 

4)接下来,我们修改DriverScript.java中关于excel的常量,用我们上面刚刚定义的常量去替换。

package executionEngine;
import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
import config.ActionsKeywords;
import config.Constants;
import utility.ExcelUtils;

import java.lang.reflect.Method;


/**
 * create by Anthony on 2018/1/30
 */
public class DriverScript {

    // 声明一个public static的类对象,所以我们可以在main方法范围之外去使用。
    public static ActionsKeywords actionsKeywords;
    public static String sActionKeyword;
    // 下面是返回类型是方法,这里用到反射类
    public static Method method[];

    // 这里我们初始化'ActionsKeywords'类的一个对象
    public DriverScript() throws NoSuchMethodException, SecurityException{
        actionsKeywords = new ActionsKeywords();
        method = actionsKeywords.getClass().getMethods();
    }

    public static void main(String[] args) throws Exception {
        // 初始化一下DriverScript类
        DriverScript ds = new DriverScript();
        String excel_path = Constants.Path_TestData;
        // 加载读取excel文件
        ExcelUtils.setExcelFile(excel_path, Constants.Sheet_TestSteps);

        for (int iRow = 1; iRow <= 9; iRow++) {
            //3表示excel中keyword对应的列的索引,也就是左边数起第4列
            sActionKeyword = ExcelUtils.getCellData(iRow, Constants.Col_ActionKeyword);
            //A调用'execute_Actions'方法
            execute_Actions();
        }

    }


    private static void execute_Actions() throws Exception {
        //循环遍历每一个关键字驱动方法(在actionskeywords.java中)
        //method variable contain all the method and method.length returns the total number of methods
        // 下面methid.length表示方法个数,method变量表示任何一个关键字方法,例如openBrowser()
        for(int i = 0;i < method.length;i++){
            //开始对比代码中关键字方法名称和excel中关键字这列值是否匹配
            if(method[i].getName().equals(sActionKeyword)){
                //一点匹配到相关关键字方法,就会调用对应的关键字静态方法
                method[i].invoke(actionsKeywords);
                //一旦任何关键字被执行,利用break语句去跳出for循环。
                break;
            }
        }
    }

}

      以上修改完成后,测试运行DriverScript.java里面的main方法,如果运行通过,说明常量替换成功,如果失败,根据报错,很容易定位到问题。到目前为止,项目的结果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值