Skip to content

Commit cb38470

Browse files
authored
Merge pull request #1 from keshavissar001/patch-1
Update README.md
2 parents 77edac6 + a8d48ad commit cb38470

File tree

1 file changed

+275
-24
lines changed

1 file changed

+275
-24
lines changed

README.md

Lines changed: 275 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,72 @@
22
![LambdaTest Logo](https://www.lambdatest.com/static/images/logo.svg)
33
---
44

5-
### Environment Setup
5+
### Running Cucumber Scripts With TestNG And Selenium
66

7-
1. Global Dependencies
8-
* Install [Maven](https://maven.apache.org/install.html)
9-
* Or Install Maven with [Homebrew](http://brew.sh/) (Easier)
10-
```
11-
$ install maven
12-
```
13-
2. Lambdatest Credentials
14-
* Set LambdaTest username and access key in environment variables. It can be obtained from [LambdaTest dashboard](https://automation.lambdatest.com/)
7+
LambdaTest Selenium Automation Grid is a cloud-based scalable Selenium testing platform which enables you to run your automation scripts on 2000+ different browsers and operating systems. You can now run your Cucumber scripts with TestNG and Selenium for automating your web application over a scalable Selenium infrastructure that is running real browsers and real operating systems.
8+
9+
10+
## Prerequisites for running tests using Cucumber automation framework :
11+
12+
## Environment Setup
13+
14+
### 1. Java Installation
15+
16+
i. For Windows :
17+
18+
You can download Java for Windows from [here](http://www.java.com/en/download/manual.jsp)
19+
20+
Run the installer and follow the setup wizard to install Java.
21+
22+
create a new JAVA_HOME environment variable and set variable value as path value to JDK folder.
23+
24+
#### This is Windows environment variable location :
25+
```
26+
Control Panel > All Control Panel Items > System > Advanced system settings > Environment Variables
27+
```
28+
29+
![altext](https://github.com/keshavissar001/images/blob/master/Img1.png)
30+
31+
ii. For Linux :
32+
33+
use this command :
34+
```
35+
sudo apt-get install openjdk-8-jre
36+
```
37+
iii. For Mac
38+
39+
Java should already be present on Mac OS X by default
40+
41+
### 2. Maven Installation
42+
43+
Install Maven from [here](https://maven.apache.org/install.html)
44+
45+
### 3 Setup
46+
47+
You can download the file. To do this click on “Clone or download” button. You can download zip file.
48+
49+
Right click on this zip file and extract files in your desired location.
50+
51+
Go to “cucumber-testng-sample-master” folder and copy its path.
52+
Open command prompt and run :
53+
54+
cd <path> (that you have copied)
55+
56+
(please ignore "<" , ">" symbols)
57+
58+
59+
![altext](https://github.com/keshavissar001/images/blob/master/SetPathCucumber.png)
60+
61+
62+
To clone the file, click on “Clone or download” button and copy the link.
63+
64+
Then open the command prompt in the folder you want to clone the file. Run the command:
65+
66+
git clone <paste link here>
67+
68+
### Lambdatest Credentials
69+
70+
Set LambdaTest username and access key in environment variables. It can be obtained from [LambdaTest dashboard](https://automation.lambdatest.com/)
1571
example:
1672

1773
- For linux/mac
@@ -25,34 +81,229 @@
2581
set LT_ACCESS_KEY="YOUR ACCESS KEY"
2682
```
2783

28-
3. Setup
29-
* checkout the repository
30-
* Check that packages are available
31-
```
32-
$ cd Cucumber-TestNG-Sample
33-
```
34-
* You may also want to run the command below to check for outdated dependencies. Please be sure to verify and review updates before editing your pom.xml file as they may not be compatible with your code.
84+
You may also want to run the command below to check for outdated dependencies. Please be sure to verify and review updates before editing your pom.xml file as they may not be compatible with your code.
85+
3586
```
3687
$ mvn versions:display-dependency-updates
3788
```
89+
90+
![altext](https://github.com/keshavissar001/images/blob/master/mvnUpdate.png)
91+
92+
### 4. Running Tests
93+
94+
Let’ start with a simple Selenium Remote Webdriver test first. The Cucumber-testng script below tests a simple to-do application with basic functionalities like mark items as done, add items in list, calculate total pending items etc.
95+
96+
Feature: Test to add item Scenario: Test sample-todo-app Given
97+
98+
I go to sample-todo-app to add item
99+
100+
Then I Click on first checkbox and second checkbox
101+
102+
Then I enter item to add When I click add button
103+
104+
Then I should verify the added item
105+
106+
Now here is the sample test file which is to be executed for the automation test through LambdaTest
107+
108+
Here is the sample feature file for Cucumber :
109+
110+
```
111+
Feature: Add new item to ToDO list
112+
113+
Scenario: Lambdatest ToDO Scenario
114+
115+
Given user is on home Page
116+
When select First Item
117+
Then select second item
118+
Then add new item
119+
Then verify added item
120+
```
121+
122+
Here is the TestRunner file to automate our feature file through Selenium using TestNG :
123+
124+
```
125+
package MyRunner;
126+
127+
import java.net.URL;
128+
129+
import org.openqa.selenium.remote.CapabilityType;
130+
import org.openqa.selenium.remote.DesiredCapabilities;
131+
import org.openqa.selenium.remote.RemoteWebDriver;
132+
import org.testng.annotations.AfterClass;
133+
import org.testng.annotations.AfterMethod;
134+
import org.testng.annotations.BeforeClass;
135+
import org.testng.annotations.BeforeMethod;
136+
import org.testng.annotations.DataProvider;
137+
import org.testng.annotations.Parameters;
138+
import org.testng.annotations.Test;
139+
140+
import cucumber.api.CucumberOptions;
141+
import cucumber.api.testng.CucumberFeatureWrapper;
142+
import cucumber.api.testng.TestNGCucumberRunner;
38143
39-
### Running Tests
144+
@CucumberOptions(
145+
features = "src/main/java/Features",
146+
glue = {"stepDefinitions"},
147+
tags = {"~@Ignore"},
148+
format = {
149+
"pretty",
150+
"html:target/cucumber-reports/cucumber-pretty",
151+
"json:target/cucumber-reports/CucumberTestReport.json",
152+
"rerun:target/cucumber-reports/rerun.txt"
153+
},plugin = "json:target/cucumber-reports/CucumberTestReport.json")
40154
41-
* To Start Test:
42-
- Navigate to Cucumber-TestNG-Sample
43-
- Run following command
155+
public class TestRunner {
156+
157+
private TestNGCucumberRunner testNGCucumberRunner;
158+
159+
public static RemoteWebDriver connection;
160+
161+
@BeforeClass(alwaysRun = true)
162+
public void setUpCucumber() {
163+
testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
164+
}
165+
166+
@BeforeMethod(alwaysRun = true)
167+
@Parameters({ "browser", "version", "platform" })
168+
public void setUpClass(String browser, String version, String platform) throws Exception {
169+
170+
String username = System.getenv("LT_USERNAME") == null ? "YOUR LT_USERNAME" : System.getenv("LT_USERNAME");
171+
String accesskey = System.getenv("LT_ACCESS_KEY") == null ? "YOUR LT_ACCESS_KEY" : System.getenv("LT_ACCESS_KEY");
172+
173+
DesiredCapabilities capability = new DesiredCapabilities();
174+
capability.setCapability(CapabilityType.BROWSER_NAME, browser);
175+
capability.setCapability(CapabilityType.VERSION,version);
176+
capability.setCapability(CapabilityType.PLATFORM, platform);
177+
178+
capability.setCapability("build", "Your Build Name");
179+
180+
capability.setCapability("network", true);
181+
capability.setCapability("video", true);
182+
capability.setCapability("console", true);
183+
capability.setCapability("visual", true);
184+
185+
String gridURL = "https://" + username + ":" + accesskey + "@hub.lambdatest.com/wd/hub";
186+
System.out.println(gridURL);
187+
connection = new RemoteWebDriver(new URL(gridURL), capability);
188+
System.out.println(capability);
189+
System.out.println(connection);
190+
}
191+
192+
@Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "features")
193+
public void feature(CucumberFeatureWrapper cucumberFeature) {
194+
testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
195+
}
196+
197+
@DataProvider
198+
public Object[][] features() {
199+
return testNGCucumberRunner.provideFeatures();
200+
}
201+
202+
@AfterClass(alwaysRun = true)
203+
public void tearDownClass() throws Exception {
204+
testNGCucumberRunner.finish();
205+
}
206+
}
207+
```
208+
209+
Below are the step definitions :
210+
211+
```
212+
package stepDefinitions;
213+
214+
import org.openqa.selenium.By;
215+
import org.openqa.selenium.remote.RemoteWebDriver;
216+
import org.testng.Assert;
217+
218+
import cucumber.api.Scenario;
219+
import cucumber.api.java.After;
220+
import cucumber.api.java.Before;
221+
import cucumber.api.java.en.Given;
222+
import cucumber.api.java.en.Then;
223+
import cucumber.api.java.en.When;
224+
import MyRunner.*;
225+
226+
public class ToDoStepDefinition extends TestRunner {
227+
228+
public RemoteWebDriver driver = this.connection;
229+
230+
@Before
231+
public void updateName(Scenario scenario) {
232+
driver.executeScript("lambda-name="+scenario.getName());
233+
}
234+
235+
@Given("^user is on home Page$")
236+
public void user_already_on_home_page() {
237+
System.out.println(driver.getCapabilities());
238+
driver.get("https://lambdatest.github.io/sample-todo-app/");
239+
240+
}
241+
242+
@When("^select First Item$")
243+
public void select_first_item() {
244+
driver.findElement(By.name("li1")).click();
245+
}
246+
247+
@Then("^select second item$")
248+
public void select_second_item() {
249+
driver.findElement(By.name("li2")).click();
250+
}
251+
252+
@Then("^add new item$")
253+
public void add_new_item() {
254+
driver.findElement(By.id("sampletodotext")).clear();
255+
driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list");
256+
driver.findElement(By.id("addbutton")).click();
257+
}
258+
259+
@Then("^verify added item$")
260+
public void verify_added_item() {
261+
String item = driver.findElement(By.xpath("/html/body/div/div/div/ul/li[6]/span")).getText();
262+
Assert.assertTrue(item.contains("Yey, Let's add it to list"));
263+
}
264+
265+
@After
266+
public void close_the_browser(Scenario scenario) {
267+
driver.executeScript("lambda-status=" + (scenario.isFailed() ? "failed" : "passed"));
268+
driver.quit();
269+
}
270+
271+
}
272+
```
273+
274+
275+
276+
To Run Your Test, use following command :
277+
44278
```
45-
To run test
46279
$ mvn test
47-
```
280+
```
281+
282+
These are the screenshots of the output :
283+
284+
![altext](https://github.com/keshavissar001/images/blob/master/CucumberResult1.png)
48285

49-
##### Routing traffic through your local machine
50-
- Set tunnel value to `true` in test capabilities
286+
![altext](https://github.com/keshavissar001/images/blob/master/CucumberResult2.png)
287+
288+
Below we see a screenshot that depicts our Cucumber-testng code is running over different browsers i.e Chrome, IE and Edge on the LambdaTest Selenium Grid Platform. The results of the test script execution along with the logs can be accessed from the LambdaTest Automation dashboard.
289+
290+
![altext](https://github.com/keshavissar001/images/blob/master/AutomationLogCucumber.png)
291+
292+
## Testing Locally Hosted or Privately Hosted Projects
293+
294+
To help you perform cross browser testing of your locally stored web pages, LambdaTest provides an SSH(Secure Shell) tunnel connection with the name Lambda Tunnel. With Lambda Tunnel, you can test your locally hosted files before you make them live over the internet. You could even perform cross browser testing from different IP addresses belonging to various geographic locations. You can also use LambdaTest Tunnel to test web-apps and websites that are permissible inside your corporate firewall.
295+
296+
* Set tunnel value to True in test capabilities
51297
> OS specific instructions to download and setup tunnel binary can be found at the following links.
52298
> - [Windows](https://www.lambdatest.com/support/docs/display/TD/Local+Testing+For+Windows)
53299
> - [Mac](https://www.lambdatest.com/support/docs/display/TD/Local+Testing+For+MacOS)
54300
> - [Linux](https://www.lambdatest.com/support/docs/display/TD/Local+Testing+For+Linux)
55301
302+
After setting tunnel you can also see the active tunnel in our LambdaTest dashboard:
303+
304+
305+
![tn](https://github.com/Apoorvlt/test/blob/master/tn.PNG)
306+
56307
### Important Note:
57308
Some Safari & IE browsers, doesn't support automatic resolution of the URL string "localhost". Therefore if you test on URLs like "http://localhost/" or "http://localhost:8080" etc, you would get an error in these browsers. A possible solution is to use "localhost.lambdatest.com" or replace the string "localhost" with machine IP address. For example if you wanted to test "http://localhost/dashboard" or, and your machine IP is 192.168.2.6 you can instead test on "http://192.168.2.6/dashboard" or "http://localhost.lambdatest.com/dashboard".
58309

0 commit comments

Comments
 (0)