Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ target

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/.idea/
13 changes: 0 additions & 13 deletions .idea/compiler.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/encodings.xml

This file was deleted.

14 changes: 0 additions & 14 deletions .idea/misc.xml

This file was deleted.

124 changes: 0 additions & 124 deletions .idea/uiDesigner.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ A simple java selenium testing script, using chrome webDriver
+ `src/main/.../utils`: some beneficial utility

## Usage

### write the URL and MACHINE type in `Constants.java`
```txt
type: windows / mac_m1
url: https://www.baidu.com
```
### Write the UI elements' xpath in `OR.txt`

> schema: `PageObject = xpath`
Expand Down
Binary file added driver/chromedriver_mac_m1
Binary file not shown.
9 changes: 5 additions & 4 deletions src/main/java/com/junit/selenium/config/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
public class Constants {

// 这里定义为public static的类型,方便其他任何类进行访问和调用
public static final String URL = "http://gc21131138.imwork.net:20430/test-maker/web/client/index.action";
public static final String URL = "https://www.baidu.com";
public static final String MACHINE = "mac_m1";
// public static final String MACHINE = "windows";

public static final String Path_TestData = ".\\src\\main\\java\\com\\junit\\selenium\\data\\data.xlsx";
public static final String File_TestData = "data.xlsx";
public static final String Path_TestData = "./src/main/java/com/junit/selenium/data/data.xlsx";


// data.xlsx中一些单元格的索引值
Expand All @@ -31,7 +32,7 @@ public class Constants {


// OR(对象仓库)文件路径
public static final String OR_Path =".\\src\\main\\java\\com\\junit\\selenium\\config\\OR.txt";
public static final String OR_Path ="./src/main/java/com/junit/selenium/config/OR.txt";

// // 测试登录用到的用户数据
// public static final String UserName = "MaoHaonan";
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/junit/selenium/config/OR.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#login page
loginUsernameInputbox=//*[@id='username']
loginPasswordInptbox =//*[@id='password']
loginAuthbtn=//*[@id='auth-button']
loginProjectlist=//*[@id="project-list"]
loginLoginbtn=//*[@id='login-button']
store=//*[@id="app"]/div[1]/div[1]/div/div[1]/div[2]/div[4]
pet=//*[@id="app"]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]
buy=//*[@id="app"]/div[1]/div[2]/div/div[1]/div[2]/div[2]/div[3]
input=//*[@id="kw"]
search=//*[@id="su"]
Binary file modified src/main/java/com/junit/selenium/data/data.xlsx
Binary file not shown.
28 changes: 19 additions & 9 deletions src/main/java/com/junit/selenium/script/DriverScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,31 @@
public class DriverScript {

private static ActionKeywords actionKeywords;
private static String sActionKeyword;//从Excel中读到的关键字
private static String[] sArgs; //输入方法的参数
public static Properties OR; //页面元素的xpath
/**
* 从Excel中读到的关键字
*/
private static String sActionKeyword;
/**
* 输入方法的参数
*/
private static String[] sArgs;
/**
* 页面元素的xpath
*/
public static Properties OR;

private static Method methods[];

private static int iTestStep;
public static boolean bResult;


private DriverScript() throws NoSuchMethodException, SecurityException, ClassNotFoundException{
private DriverScript() throws SecurityException{
actionKeywords = new ActionKeywords();
methods = actionKeywords.getClass().getMethods();
}

public static void main(String[] args) throws Exception{
public static void main(String[] args) {
try {
DOMConfigurator.configure("log4j.xml");

Expand All @@ -37,10 +46,10 @@ public static void main(String[] args) throws Exception{
Log.info("加载和读取Excel数据文件");
ExcelUtils.setExcelFile(Constants.Path_TestData);

FileInputStream fs = new FileInputStream(Constants.OR_Path);// 创建一个文件输入流对象
OR = new Properties(System.getProperties()); // 创建一个Properties对象
FileInputStream fs = new FileInputStream(Constants.OR_Path);
OR = new Properties(System.getProperties());

OR.load(fs);// 加载全部对象仓库文件
OR.load(fs);

DriverScript startEngine = new DriverScript();
Log.info("开始执行测试用例。");
Expand Down Expand Up @@ -124,7 +133,8 @@ private static void execute_Actions() throws Exception {

// 一但匹配到相关关键字方法,就会调用对应的关键字静态方法
// methods[i].invoke(actionKeywords);
method.invoke(actionKeywords, sArgs); //如果元素对象是空的,则excel中必须有个空格字符,否则java会编译不通过。
//如果元素对象是空的,则excel中必须有个空格字符,否则java会编译不通过。
method.invoke(actionKeywords, sArgs);

// 一旦任何关键字被执行,利用break语句去跳出for循环。
// This code block will execute after every test step
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/junit/selenium/utils/ChromeDriverUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.junit.selenium.utils;

import com.junit.selenium.config.Constants;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.File;
Expand All @@ -13,8 +14,14 @@ public class ChromeDriverUtils {


private static void createAndStartService() throws IOException {
String driverPath = "";
if(Constants.MACHINE.equals("mac_m1")){
driverPath = "driver/chromedriver_mac_m1";
}else{
driverPath = "driver/chromedriver.exe";
}
service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("driver/chromedriver.exe"))
.usingDriverExecutable(new File(driverPath))
.usingAnyFreePort()
.build();
service.start();
Expand All @@ -27,7 +34,7 @@ private static void stopService() {
public static WebDriver createDriver() throws IOException {
createAndStartService();
WebDriver driver = new RemoteWebDriver(service.getUrl(),
DesiredCapabilities.chrome());
new ChromeOptions());
return driver;
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/junit/selenium/ChromeDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ChromeDriverTest extends TestCase {
// @BeforeClass
// public static void createAndStartService() throws IOException {
// service = new ChromeDriverService.Builder()
// .usingDriverExecutable(new File("driver/chromedriver.exe"))
// .usingDriverExecutable(new File("driver/chromedriver_mac_m1.exe"))
// .usingAnyFreePort()
// .build();
// service.start();
Expand All @@ -46,6 +46,6 @@ public void testBingSearch() {
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("webdriver");

assertEquals("微软 Bing 搜索 - 国内版", driver.getTitle());
assertEquals("必应", driver.getTitle());
}
}