Multi Browser Test
In many case of testing , we need enable our script running on cross browser to make sure that the front-end of website is stable with multi browser.In this tutorial , you will know step by step to run script in 3 browser basic is ‘Chrome’ , ‘IE’ and ‘FireFox’ with this action :
Login Facebook
Post Status
How to do it….
3) Right click into project –> New –> folder –> name it as ‘Driver’
4) Unzip and Copy 2 driver Chrome and IE to folder ‘Driver’.
5) Double click on ‘pom.xml’ paste the configuration below the tag ‘dependencies’ to enable Maven automatic get the Directory of project when build project.
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) After that your ‘pom.xml’ file will look like this :
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) Right click in folder ‘src’ –> New –> Folder –> name it as ‘template’
8) Right click in folder ‘template’ –> New –> Folder –> name it as ‘resources’
9) Right click in folder ‘resources’ –> New –> File –> name it as ‘system.properties’
10) Open file ‘system.properties’ and copy the code below and paste to this file:
1
2
project.basedir=${project.basedir}
project.build.directory=${project.build.directory}
11) Right click in your project –> Run as –> Maven generate-resources.
12) Right click in ‘src/main/java’ –> New –> Package –> name it as ‘com.selenium.util’
13) Right click in package ‘com.selenium.util’ –> New –> Class –> name it as ‘DriverUtil’
14) Double click on ‘DriverUtil.java’ and paste the below code to it.
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) Double click on ‘autoTestFacebook.java’ and paste the code below to it.
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) Right Click in Project –> New –> File –> name it as ‘TestNg.xml’
17) Copy code below and paste to ‘TestNg.xml’. Remember to change the username and password in tag parameter. You can change the value of parallel to test –> it will run 3 browser in the same time.
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="browser" value="firefox" />
<parameter name="status" value="firefox" />
<parameter name="username" value="yourusername" />
<parameter name="password" value="yourpassword" />
<classes>
<class name="com.selenium.test.autoTestFacebook" />
</classes>
</test>
<test name="IETest">
<parameter name="username" value="yourusername" />
<parameter name="password" value="yourpassword" />
<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="yourusername" />
<parameter name="password" value="yourpassword" />
<parameter name="browser" value="chrome" />
<parameter name="status" value="chrome" />
<classes>
<class name="com.selenium.test.autoTestFacebook" />
</classes>
</test>
</suite>
17) Right click in file ‘TestNG.xml’ –> Run as –> TestNG Suite and wait for the process running.
18) Open folder ‘Test-Output’ –> Open folder ‘Suite’ – > You will see 3 file html –> open to see the report of each browser.
Note : In this tutorial we have many knowledge so I recommend you to read the comment of code to know what it do.
Post a Comment
Post a Comment