Skip to content

Initializing seleniumJavaExperiment #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
80 changes: 80 additions & 0 deletions seleniumJavaExperiment/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Everything in the .idea directory
.idea/*

# CMake
cmake-build-*/

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

### Java ###
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# End of https://www.toptal.com/developers/gitignore/api/intellij,macos,java
3 changes: 3 additions & 0 deletions seleniumJavaExperiment/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions seleniumJavaExperiment/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions seleniumJavaExperiment/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions seleniumJavaExperiment/.idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions seleniumJavaExperiment/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions seleniumJavaExperiment/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions seleniumJavaExperiment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<div id="top"></div>

<!-- PROJECT SHIELDS -->
[![selenium.dev](https://img.shields.io/badge/Selenium-43B02A?style=for-the-badge&logo=Selenium&logoColor=white)](https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java)
[![java.jdk](https://img.shields.io/badge/Java-ED8B00?style=for-the-badge&logo=java&logoColor=white)](https://www.oracle.com/java/technologies/downloads/#jdk17-mac)

<!-- PROJECT LOGO -->
<br />
<div align="center">
<p align="center">
<a href="https://www.selenium.dev/"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/Selenium_logo.svg/1280px-Selenium_logo.svg.png"/></a>
</p>

<h3 align="center">Selenium Java Experiment</h3>
</div>

<!-- ABOUT THE PROJECT -->
## About The Project

The main goal of this experiment is to share with the community how `Selenium Webdriver` works along with Java as a testing framework and to learn how nowadays test automation projects are set up and worked utilizing the latest design patterns and frameworks, e.g. TestNG. This project has been built using POM / Page Factory, read [this article](https://www.browserstack.com/guide/page-object-model-in-selenium) to learn more. The testing subject for this experiment is this website — [http://the-internet.herokuapp.com/](http://the-internet.herokuapp.com/).

<p align="right">(<a href="#top">back to top</a>)</p>

<!-- GETTING STARTED -->
## Getting Started

### Prerequisites
This is a Maven project, create the following under `<dependencies>` in your `pom.xml` file if they are not there already.

* Selenium Webdriver
```xml
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.1</version>
</dependency>
```
* TestNG Framework
```xml
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
```

* Selenium Webdriver for Google Chrome v 97.0.4692.99. Download from [here](https://chromedriver.storage.googleapis.com/index.html?path=97.0.4692.71/) if needed.

<p align="right">(<a href="#top">back to top</a>)</p>

<!-- CONTACT -->
## Contact
Asim Khan - [@asimkhan](mailto:"[email protected]")

Israel Alfaro - [@israelalfaro](mailto:"[email protected]")

Pedro Hyvo - [@pedrohyvo](https://www.linkedin.com/in/pedrohyvo/)

<p align="right">(<a href="#top">back to top</a>)</p>
31 changes: 31 additions & 0 deletions seleniumJavaExperiment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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>seleniumJavaExperiment</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.5</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
86 changes: 86 additions & 0 deletions seleniumJavaExperiment/src/test/java/SeleniumTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import pageObjects.ABtestPage;
import pageObjects.CheckboxesPage;
import pageObjects.DropDownPage;
import pageObjects.HomePage;

import java.io.PrintStream;

public class SeleniumTests {

WebDriver driver;

HomePage homePage;
ABtestPage abTestPage;
CheckboxesPage checkboxesPage;
DropDownPage dropdownpage;

@BeforeClass
public void beforeClass () {
System.setProperty("webdriver.chrome.driver", "./src/test/resources/chromedriver/chromedriver.exe");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make use of webdriver binaries -
Automatically download WebDriver Binaries

Let me know if you need any help here.

Copy link

@asimkm asimkm Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kenil-fadia I need your help to resolve this.

driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://the-internet.herokuapp.com/");
homePage = PageFactory.initElements(driver, HomePage.class);
abTestPage = PageFactory.initElements(driver, ABtestPage.class);
checkboxesPage = PageFactory.initElements(driver, CheckboxesPage.class);
dropdownpage = PageFactory.initElements(driver, DropDownPage.class);
}
public void navigateBackHome(){
driver.get("http://the-internet.herokuapp.com/");
}


@Test(priority = 0)
public void testNavigationToABtestPage () {
homePage.clickOption("A/B Testing");
Assert.assertEquals("A/B Test Control", abTestPage.getTitleText());

}



@Test(priority = 1)
public void testNavigationToCheckboxesPage () throws InterruptedException {
navigateBackHome();
homePage.clickOption("Checkboxes");
Assert.assertEquals("Checkboxes", checkboxesPage.getTitleText());
Thread.sleep(1000);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a demo repo it's not critical, but we should avoid these when possible.

}

@Test(priority = 2)
public void clickCheckboxes(){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the tests should be independent of each other.

checkboxesPage.clickCheckbox();
Assert.assertTrue(checkboxesPage.getCheckboxState1());
}
@Test(priority = 3)
public void unclickCheckbox2(){
checkboxesPage.unclickSecondCheckbox();
Assert.assertFalse(checkboxesPage.getCheckboxState2());
}
@Test(priority = 4)
public void testNavigationToDropdownPage () {
navigateBackHome();
homePage.clickOption("Dropdown");
Assert.assertEquals("Dropdown List", dropdownpage.getTitleText());
dropdownpage.openDropDown();
dropdownpage.selectOptions("Option 2");
dropdownpage.openDropDown();
System.out.println(dropdownpage.returnDropdownValue());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid logs if possible.

Assert.assertEquals("Option 2", dropdownpage.returnDropdownValue());

}

@AfterClass
public void afterClass () {
driver.quit();
}
}
14 changes: 14 additions & 0 deletions seleniumJavaExperiment/src/test/java/pageObjects/ABtestPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pageObjects;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class ABtestPage {

@FindBy (css = ".example h3")
WebElement pageTitle;

public String getTitleText () {
return pageTitle.getText();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pageObjects;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class CheckboxesPage {

@FindBy(css = ".example h3")
WebElement pageTitle;
@FindBy(xpath = "/html/body/div[2]/div/div/form/input[1]")
WebElement check1;
@FindBy(xpath = "/html/body/div[2]/div/div/form/input[2]")
WebElement check2;

public String getTitleText () {
return pageTitle.getText();
}


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unwanted line breaks.

public void clickCheckbox(){
check1.click();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the indentation

}

public boolean getCheckboxState1(){return check1.isSelected();}

public void unclickSecondCheckbox(){check2.click();}

public boolean getCheckboxState2(){return check2.isSelected();}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, not critical for a demo repo, but for the next iteration we could refactor these because a lot of the code is repeating.



}
Loading