Довольно часто приходится проверять кросс-браузерность сайта. В этом уроке будет рассматриваться запуск тестов в Chrome, IE и FireFox для входа в Facebook.
Как это сделать…
1) Скачайте драйвер для IE для 32/64 битной системы: http://www.seleniumhq.org/download/
2) Скачайте драйвер для Chrome: http://chromedriver.storage.googleapis.com/index.html
3) Нажмите правой кнопкой на проект и выберите New –> folder –> назовите ее “Driver”.
4) Разархивируйте и добавьте драйвера в созданную директорию “Driver”.
5) Откройте “pom.xml” и добавьте следующую конфигурацию ниже тега “dependencies”, чтобы Maven мог автоматически найти директорию проекта при сборке проекта.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<reporting> <plugins> <!-- <plugin> --> <!-- <groupId>org.codehaus.sonar-plugins</groupId> --> <!-- <artifactId>maven-report</artifactId> --> <!-- <version>0.1</version> --> <!-- </plugin> --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-report-plugin</artifactId> <version>2.14.1</version> <configuration> <!-- <showSuccess>false</showSuccess> --> </configuration> </plugin> </plugins> </reporting> <build> <extensions> <!-- start - for deploying using webdav --> <extension> <groupId>org.apache.maven.wagon</groupId> <artifactId>wagon-webdav</artifactId> <version>1.0-beta-2</version> </extension> <!-- end - for deploying using webdav --> </extensions> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>init-res</id> <!-- use the copy resources instead of resources, which adds it to the eclipse buildpath --> <phase>initialize</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.basedir}/src/main/resources</outputDirectory> <resources> <resource> <directory>${project.basedir}/src/template/resources</directory> <targetPath>${project.basedir}/src/main/resources</targetPath> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.7.1</version> <configuration> <argLine>-Xmx1024m -Xms1024m</argLine> <suiteXmlFiles> <suiteXmlFile>Test.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>sonar-maven-plugin</artifactId> <version>2.1</version> </plugin> </plugins> </build> |
6) После этого “pom.xml” будет выглядеть следующим образом:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
<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>Selenium-Automation-Framework</groupId> <artifactId>Selenium-Framework</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.41.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-server</artifactId> <version>2.41.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-firefox-driver</artifactId> <version>2.41.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-chrome-driver</artifactId> <version>2.41.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-remote-driver</artifactId> <version>2.41.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-ie-driver</artifactId> <version>2.41.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.8</version> </dependency> </dependencies> <reporting> <plugins> <!-- <plugin> --> <!-- <groupId>org.codehaus.sonar-plugins</groupId> --> <!-- <artifactId>maven-report</artifactId> --> <!-- <version>0.1</version> --> <!-- </plugin> --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-report-plugin</artifactId> <version>2.14.1</version> <configuration> <!-- <showSuccess>false</showSuccess> --> </configuration> </plugin> </plugins> </reporting> <build> <extensions> <!-- start - for deploying using webdav --> <extension> <groupId>org.apache.maven.wagon</groupId> <artifactId>wagon-webdav</artifactId> <version>1.0-beta-2</version> </extension> <!-- end - for deploying using webdav --> </extensions> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>init-res</id> <!-- use the copy resources instead of resources, which adds it to the eclipse buildpath --> <phase>initialize</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.basedir}/src/main/resources</outputDirectory> <resources> <resource> <directory>${project.basedir}/src/template/resources</directory> <targetPath>${project.basedir}/src/main/resources</targetPath> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.7.1</version> <configuration> <argLine>-Xmx1024m -Xms1024m</argLine> <suiteXmlFiles> <suiteXmlFile>Test.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>sonar-maven-plugin</artifactId> <version>2.1</version> </plugin> </plugins> </build> </project> |
7) Создайте новую папку в “src” –> New –> Folder –> и назовите ее “template”:
8) Создайте папку в “template” –> New –> Folder –> и назовите ее “resources”:
9) Создайте файл свойств в “resources”–> New –> File –> назовите его “system.properties”:
10) Скопируйте следующий код в “system.properties”:
1 2 |
project.basedir=${project.basedir} project.build.directory=${project.build.directory} |
11) Соберите проект нажав на проект –> Run as –> Maven generate-resources.
12) Создайте новый пакет в “src/main/java” –> New –> Package –> и назовите его “com.selenium.util”:
13) Создайте новый класс в “com.selenium.util” –> New –> Class –> и назовите его “DriverUtil”:
14) Откройте “DriverUtil.java” и скопируйте в него следующий код.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
package com.selenium.util; import java.io.File; import java.io.IOException; import java.util.Properties; public class DriverUtil { public static final String PROP_PROJECT_BASE_DIR = "project.basedir"; public static final String FOLDER_DRIVER = "Driver"; public static final String DEFAULT_PROPERTIES = "system.properties"; private static Properties prod; /** * @return the path of ie driver file. */ public static String getIeDriver(){ String path = getKey(PROP_PROJECT_BASE_DIR) + File.separator + FOLDER_DRIVER + File.separator + "IEDriverServer.exe"; try { File driverIe = new File(path); if(driverIe.exists()){ return driverIe.getAbsolutePath(); } } catch (Exception e) { e.printStackTrace(); return null; } return null; } /** * @return the path of chrome driver file */ public static String getChromeDriver(){ String path = getKey(PROP_PROJECT_BASE_DIR) + File.separator + FOLDER_DRIVER + File.separator + "chromedriver.exe"; try { File driverChrome = new File(path); if(driverChrome.exists()){ return driverChrome.getAbsolutePath(); } } catch (Exception e) { e.printStackTrace(); return null; } return null; } /** * @return load the file system.properties */ public static Properties getProperties() { if (prod == null) { prod = new Properties(); try { prod.load(DriverUtil.class.getClassLoader().getResourceAsStream(DEFAULT_PROPERTIES)); } catch (IOException e) { // } } return prod; } /** * @param key * @return get value of key */ public static String getKey(String key) { Object obj = getProperties().get(key); String value = ""; if (obj != null) value = obj.toString(); return value; } } |
15) Скопируйте следующий код в “autoTestFacebook.java”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package com.selenium.test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.testng.annotations.Parameters; import org.testng.annotations.Test; import org.testng.annotations.BeforeTest; import org.testng.annotations.AfterTest; import com.selenium.pageObject.LoginPage; import com.selenium.userAction.PostStatus; import com.selenium.userAction.SignIn; import com.selenium.util.DriverUtil; public class autoTestFacebook { /** * Create WebDriver as static variable */ private static WebDriver driver; private static String username_login; private static String password_login; private static String status_facebook; /** * Setup some variable to run your script test, load from the file TestNg.xml * Your parameters in annotation Parameters will match with define in TestNG.xml */ @Parameters({"browser","status","username","password"}) @BeforeTest public void beforeTest(String browser, String status, String username, String password) { if(browser.equalsIgnoreCase("firefox")) { driver = new FirefoxDriver(); }else if (browser.equalsIgnoreCase("ie")) { // Here I am setting up the path for my IEDriver System.setProperty("webdriver.ie.driver", DriverUtil.getIeDriver()); driver = new InternetExplorerDriver(); }else if (browser.equalsIgnoreCase("chrome")){ System.setProperty("webdriver.chrome.driver", DriverUtil.getChromeDriver()); driver = new ChromeDriver(); } username_login = username; status_facebook = status; password_login = password; } /** * your test script call action class here */ @Test public void f() { LoginPage.loadPage(driver); SignIn.Execute(driver, username_login, password_login); PostStatus.Execute(driver, status_facebook); } /** * after run your script test , use this code to close your browser */ @AfterTest public void afterTest() { driver.quit(); } } |
16) Создайте файл для TestNG в корне проекта–> New –> File –> и назовите его “TestNg.xml”
17) Скопируйте следующий код в “TestNg.xml”. Также Вы можете изменить значение “parallel” на “test” –> это запустит 3 браузера в одно время.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" parallel="none"> <test name="FirefoxTest"> <parameter name="username" value="autoqaorg@gmail.com" /> <parameter name="password" value="autoqapassword" /> <parameter name="browser" value="firefox" /> <parameter name="status" value="firefox" /> <classes> <class name="com.selenium.test.autoTestFacebook" /> </classes> </test> <test name="IETest"> <parameter name="username" value="autoqaorg@gmail.com" /> <parameter name="password" value="autoqapassword" /> <parameter name="browser" value="ie" /> <parameter name="status" value="ie" /> <classes> <class name="com.selenium.test.autoTestFacebook" /> </classes> </test> <test name="ChromeTest"> <parameter name="username" value="autoqaorg@gmail.com" /> <parameter name="password" value="autoqapassword" /> <parameter name="browser" value="chrome" /> <parameter name="status" value="chrome" /> <classes> <class name="com.selenium.test.autoTestFacebook" /> </classes> </test> </suite> |
17) Запустите “TestNG.xml”–> Run as –> TestNG Suite.
18) После завершения выполнения откройте “Test-Output” –> Oткройте директорию “Suite” – > Вы увидите 3 html файла –> можете открыть и просмотреть отчет.