1. 程式人生 > >使用Spring 定時器 + selenium打卡

使用Spring 定時器 + selenium打卡

使用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>