使用Spring 定时器 + selenium操作页面
**引入Spring 的配置,开启定时任务器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
">
<!-- 开启定时器 -->
<task:annotation-driven />
</beans>
java 代码:
package com.demo.simpledemo.autotest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class AutoTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//如果测试的浏览器没有安装在默认目录,那么必须在程序中设置
//bug1:System.setProperty("webdriver.chrome.driver", "C://Program Files (x86)//Google//Chrome//Application//chrome.exe");
//bug2:System.setProperty("webdriver.chrome.driver", "C://Users//Yoga//Downloads//chromedriver_win32//chromedriver.exe");
System.setProperty("webdriver.chrome.driver", "D:\\UT\\new_chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.baidu.com");
// 获取 网页的 title
System.out.println("The testing page title is: " + driver.getTitle());
// driver.quit();
}
@Scheduled(cron="0 59 9 ? * *") //每天9点59 分执行
private void myTest(){
System.out.println("打卡上班");
// autoClockIn();
}
}
pom文件引入
<!-- auto test jar -->
<dependency>
<groupId>org.openqa.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.48.0</version>
<type>jar</type>
<scope>system</scope>
<systemPath>D:\workspaces\simpleDemo\SimpleDemo\autojar\selenium-java-2.48.0.jar</systemPath>
</dependency>
<dependency>
<groupId>org.openqa.selenium</groupId>
<artifactId>selenium-java-srcs</artifactId>
<version>2.48.0</version>
<type>jar</type>
<scope>system</scope>
<systemPath>D:\workspaces\simpleDemo\SimpleDemo\autojar\selenium-java-2.48.0-srcs.jar</systemPath>
</dependency>
<dependency>
<groupId>com.google.gson</groupId>
<artifactId>gson</artifactId>
<version>2.1</version>
<type>jar</type>
<scope>system</scope>
<systemPath>D:\workspaces\simpleDemo\SimpleDemo\autojar\gson-2.1.jar</systemPath>
</dependency>
<dependency>
<groupId>com.google.gson</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
<type>jar</type>
<scope>system</scope>
<systemPath>D:\workspaces\simpleDemo\SimpleDemo\autojar\guava-18.0.jar</systemPath>
</dependency>
<dependency>
<groupId>com.google.gson</groupId>
<artifactId>guava-r09</artifactId>
<version>r09</version>
<type>jar</type>
<scope>system</scope>
<systemPath>D:\workspaces\simpleDemo\SimpleDemo\autojar\guava-r09.jar</systemPath>
</dependency>