• Selenium Video Tutorials

Selenium WebDriver 安装



Selenium是一个自动化测试工具,帮助我们自动化Web应用程序的测试。在安装Selenium WebDriver之前,我们首先需要确定使用哪种语言来开发Selenium测试,因为有多种语言支持Selenium。

使用Java安装Selenium WebDriver

步骤1 - 我们需要下载并安装Java到本地系统。可以通过访问以下链接完成:Java 下载

成功安装Java后,我们可以在命令提示符中运行命令:java 来确认安装。

C:\java 

屏幕上将显示以下信息:

Usage: java [options] <mainclass> [args...]
           (to execute a class)
   or  java [options] -jar <jarfile> [args...]
           (to execute a jar file)
   or  java [options] -m <module>[/<mainclass>] [args...]
       java [options] --module <module>[/<mainclass>] [args...]
           (to execute the main class in a module)
   or  java [options] <sourcefile> [args]
           (to execute a single source-file program)

 Arguments following the main class, source file, -jar <jarfile>,
 -m or --module <module>/<mainclass> are passed as the arguments to
 main class.

 where options include:

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
    --class-path <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -p <module path>
    --module-path <module path>...
                  A ; separated list of directories, each directory
                  is a directory of modules.
    --upgrade-module-path <module path>...
                  A ; separated list of directories, each directory
                  is a directory of modules that replace upgradeable
                  modules in the runtime image
    --add-modules <module name>[,<module name>...]
                  root modules to resolve in addition to the initial module.
                  <module name> can also be ALL-DEFAULT, ALL-SYSTEM,
                  ALL-MODULE-PATH.
    --enable-native-access <module name>[,<module name>...]
                  modules that are permitted to perform restricted native operations.
                  <module name> can also be ALL-UNNAMED.
    --list-modules
                  list observable modules and exit
    -d <module name>
    --describe-module <module name>
                  describe a module and exit
    --dry-run     create VM and load main class but do not execute main method.
                  The --dry-run option may be useful for validating the
                  command-line options such as the module system configuration.
    --validate-modules
                  validate all modules and exit
                  The --validate-modules option may be useful for finding
                  conflicts and other errors with modules on the module path.
    -D<name>=<value>
                  set a system property
    -verbose:[class|module|gc|jni]
                  enable verbose output for the given subsystem
    -version      print product version to the error stream and exit
    --version     print product version to the output stream and exit
    -showversion  print product version to the error stream and continue
    --show-version
                  print product version to the output stream and continue
    --show-module-resolution
                  show module resolution output during startup
    -? -h -help
                  print this help message to the error stream
    --help        print this help message to the output stream
    -X            print help on extra options to the error stream
    --help-extra  print help on extra options to the output stream
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:jdwp
                  see also -agentlib:jdwp=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image
                  HiDPI scaled images are automatically supported and used
                  if available. The unscaled image filename, e.g. image.ext,
                  should always be passed as the argument to the -splash option.
                  The most appropriate scaled image provided will be picked up
                  automatically.
                  See the SplashScreen API documentation for more information
    @argument files
                  one or more argument files containing options
    --disable-@files
                  prevent further argument file expansion
    --enable-preview
                  allow classes to depend on preview features of this release
To specify an argument for a long option, you can use --<name>=<value> or
--<name> <value>.

接下来,我们将通过运行以下命令来确认已安装的Java版本:

java –version

将显示以下输出:

openjdk version "17.0.9" 2023-10-17
OpenJDK Runtime Environment Homebrew (build 17.0.9+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.9+0, mixed mode, sharing)

执行的命令输出将表示系统中安装的Java版本。

要更详细地了解如何设置Java,请参考以下链接:Java环境设置

步骤2 - 使用以下链接在系统中安装Maven:下载Apache Maven

接下来,我们将通过运行以下命令来确认已安装的Maven版本:

mvn –version

将显示以下输出:

Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)
Maven home: /opt/homebrew/Cellar/maven/3.9.6/libexec
Java version: 21.0.1, vendor: Homebrew, runtime: /opt/homebrew/Cellar/openjdk/21.0.1/libexec/openjdk.jdk/Contents/Home
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "14.0", arch: "aarch64", family: "mac"

执行的命令输出将表示系统中安装的Maven版本。

步骤3 - 安装IntelliJ编辑器来运行Selenium测试。市场上有许多可用的编辑器,例如:Eclipse,IntelliJ,Atom等等。使用这些编辑器,我们可以开始一个项目,开始在Selenium上进行测试自动化。

步骤4 - IntelliJ编辑器设置应已成功完成。

步骤5 - 在Main.java文件中添加以下代码。

示例1

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;

public class Main {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new EdgeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      
      // URL launch 
      driver.get("https://tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // get browser title after browser launch
      System.out.println("Browser title: " + driver.getTitle());

   }
}

在pom.xml文件中添加的依赖项:

<?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>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   
   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.19.0</version>
      </dependency>
   </dependencies>
</project>

步骤6 - 运行测试并等待执行完成

步骤7 - Edge浏览器启动,我们在控制台中得到了输出消息 - 浏览器标题:Selenium实践 - 学生注册表单进程已完成,退出代码为0,表示代码成功执行。

输出

Browser title: Google

Process finished with exit code 0

同时,Edge浏览器启动时顶部显示消息Edge正在被自动化测试软件控制

示例2

让我们来看另一个例子,我们将启动浏览器,打开一个URL,获取浏览器标题,最后退出浏览器。

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;

public class MainBrowserQuit {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new EdgeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // URL launch and get the browser title
      driver.get("https://tutorialspoint.com/selenium/practice/selenium_automation_practice.php");
      System.out.println( "Browser title after launch: " + driver.getTitle());
      
      // close browser
      driver.quit();
   }
}

输出

Browser title after launch: Selenium Practice - Student Registration Form

Process finished with exit code 0

在上面的例子中,我们首先启动了Edge浏览器,然后检索了浏览器标题,然后退出了浏览器,在控制台中收到了消息 - 启动后浏览器标题:Selenium实践 - 学生注册表单

最后,收到了消息进程已完成,退出代码为0,表示代码成功执行。

结论

本教程对Selenium安装进行了全面讲解。我们从介绍使用Java安装Selenium WebDriver开始,并通过示例说明如何在Selenium WebDriver中启动浏览器然后退出浏览器。这使您能够深入了解Selenium的安装。建议您多加练习所学内容,并探索其他与Selenium相关的知识,以加深理解并拓宽视野。

广告