在linux下配置selenium实现web自动化

1. 环境准备

1.1 环境信息

VNC (Ubuntu)

1.2 安装 web broswer和web driver

  • Chrome
#chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb
#Driver
https://chromedriver.chromium.org/

unzip chromedriver_linux64.zip
sudo cp chromedriver /usr/local/bin/chromedriver
sudo chmod a+x /usr/local/bin/chromedriver
  • Firefox
#Download web site
https://www.mozilla.org/en-US/firefox/all/#product-desktop-release
#Driver
https://github.com/mozilla/geckodriver

tar -xvzf geckodriver-v0.29.0-linux64.tar.gz
sudo cp geckodriver /usr/local/bin/geckodriver
sudo chmod a+x /usr/local/bin/geckodriver

1.3 安装python3和selenium

#install python3
wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz
tar xvzf Python-3.9.2.tgz
cd Python-3.9.2
./configure --prefix=/usr/local/python3
make & make install

#install selenium via pip3
sudo pip3 install selenium

2. 测试脚本

2.1 python

  • chrome
#!/usr/bin/python3

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

url = "http://23.0.48.1/selenium_Test.html"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--autoplay-policy=no-user-gesture-required')
chrome_options.add_argument('--disk-cache-size=0')
chrome_options.add_argument('disable-quic')
#only for 'root' user
chrome_options.add_argument('--no-sandbox')
#remove the default proxy from VNC
chrome_options.add_argument('--no-proxy-server')
curVal = 20

def do_browsing_for_config_period(url, duration):
    browser = webdriver.Chrome(options=chrome_options)
    browser.get(url)
    time.sleep(duration)
    quality = browser.find_element(By.ID, "quality").text.split(':')[1]
    start_time = browser.find_element(By.ID, "bufferTime").text.split(':')[1]
    print("quality: %s, bufferTime: %s ms" % (quality, start_time))
    browser.close()

print("Run single browsing case for %s seconds" % curVal)
do_browsing_for_config_period(url, int(curVal))
  • firefox
#!/usr/bin/python3

import time
from selenium import webdriver
from selenium.webdriver.common.by import By

url = "http://23.0.48.1/selenium_Test.html"
duration = 20

brower = webdriver.Firefox()
brower.get(url)
time.sleep(duration)
quality = browser.find_element(By.ID, "quality").text.split(':')[1]
start_time = browser.find_element(By.ID, "bufferTime").text.split(':')[1]
print("quality: %s, bufferTime: %s ms" % (quality, start_time))
browser.close()

2.2 java

  • chrome
package ericsson.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.*;

public class chromeDriver {
    WebDriver driver;
    String proxy = "www-proxy.xxx-corporation.se:8080";
    ChromeOptions options = new ChromeOptions().addArguments("--proxy-server=http://" + proxy);
    String google = "http://www.google.com";
    String baidu = "http://www.baidu.com";
    boolean internal = true;

    @BeforeMethod
    public void beforeMethod() {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\HandsomeBoy\\MSP\\Software\\chromedriver_win32\\chromedriver.exe");
        System.out.println("system environment variable webdriver.chrome.driver = " + System.getProperty("webdriver.chrome.driver"));
    }

    @AfterMethod
    public void afterMethod() {
        System.out.println("Page title is: " + driver.getTitle());
        driver.quit();
    }

    @Test
    public void test_case3() throws Exception{
        WebElement element = null;
        if (internal){
            driver = new ChromeDriver();
            driver.get(baidu);
            element = driver.findElement(By.id("kw"));
        }else {
            driver = new ChromeDriver(options);
            driver.get(google);
            element = driver.findElement(By.name("q"));
        }
        element.sendKeys("12306");
        element.submit();
        Thread.sleep(5000);
    }
}
<!-- maven pom file -->

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>seleniumTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.141.59</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
        </dependency>
    </dependencies>
</project>

Note: the Firefox can not play the mp4 video, so i decide to use the Chrome for test instead of Firefox.

3. Web server

For test the mp4 video auto play once the selenium get the video content, A html for test have to be prepared in advacne.

  • selenium_Test.html
<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>video start up system test</title> 
</head>
<body>
    <h1> Gold Subscriber </h1>
    <p id="quality">Current Quality: </p>
    <p id="buffer">Buffer events: </p>
    <p id="bufferTime">Total buffering time (ms): </p>
    <p id="changes">N changes: </p>
<video width="320" height="240" muted controls="controls" autoplay="autoplay">
  <source src="http://23.0.48.1/video/wMFKYahslt8.mp4" type="video/mp4">
</video>
</body>
</html>

In aboved code, muted controls="controls" autoplay="autoplay" is mandatory for html for auto-play once the ready is ready.

4. Demo

selenium_demo

posted @ 2021-03-08 12:05  无知是恶  阅读(640)  评论(0编辑  收藏  举报