Last active
January 20, 2017 07:03
-
-
Save upgundecha/4bf4b1cbce0a357faaec934ee7bf67bf to your computer and use it in GitHub Desktop.
SeleniumBootCamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.shipwreck.page; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.support.FindBy; | |
import org.openqa.selenium.support.How; | |
import org.openqa.selenium.support.PageFactory; | |
/** | |
* This is Shipwreck Home Page | |
* Created by IEUser on 1/19/2017. | |
*/ | |
public class HomePage { | |
@FindBy(how = How.LINK_TEXT, linkText = "Home") | |
private WebElement homeLink; | |
@FindBy(how = How.LINK_TEXT, linkText = "Shipwrecks") | |
private WebElement shipwrecksLink; | |
@FindBy(how = How.LINK_TEXT, linkText = "Click Here! »") | |
private WebElement clickHere; | |
@FindBy(how = How.TAG_NAME, tagName = "H2") | |
private WebElement banner; | |
private WebDriver driver; | |
public HomePage(WebDriver driver) { | |
this.driver = driver; | |
PageFactory.initElements(driver, this); | |
} | |
/** | |
* This will check if Home page is displayed | |
* @return | |
*/ | |
public boolean isLoaded() { | |
return banner.getText() | |
.contains("Das Boot - WWII Shipwreck Dive Locations"); | |
} | |
/** | |
* This will open Shipwreck Page | |
*/ | |
public ShipwreckPage openShipwreckPage() { | |
shipwrecksLink.click(); | |
return new ShipwreckPage(driver); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.shipwreck.test; | |
import com.example.common.BaseTest; | |
import com.example.shipwreck.page.HomePage; | |
import com.example.shipwreck.page.ShipwreckPage; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.support.PageFactory; | |
import org.testng.annotations.Test; | |
import static org.hamcrest.Matchers.equalTo; | |
import static org.hamcrest.Matchers.greaterThan; | |
import static org.hamcrest.Matchers.is; | |
import static org.hamcrest.junit.MatcherAssert.assertThat; | |
/** | |
* Created by IEUser on 1/19/2017. | |
*/ | |
public class ShipwreckAppTest extends BaseTest { | |
@Test | |
public void homePageTest() { | |
HomePage homePage = | |
PageFactory.initElements(getDriver(), HomePage.class); | |
assertThat(homePage.isLoaded(), is(true)); | |
} | |
@Test | |
public void shipWreckPageTest() { | |
HomePage homePage = | |
PageFactory.initElements(getDriver(), HomePage.class); | |
ShipwreckPage shipwreckPage = homePage.openShipwreckPage(); | |
assertThat(shipwreckPage.isLoaded(), is(true)); | |
} | |
@Test | |
public void addShipWreck() { | |
HomePage homePage = | |
PageFactory.initElements(getDriver(), HomePage.class); | |
ShipwreckPage shipwreckPage = homePage.openShipwreckPage(); | |
shipwreckPage.addNewShipwreck("Test", "Test", "Good", "2000", "3000", "10", "10"); | |
assertThat(shipwreckPage.recordCount(), equalTo(1)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.shipwreck.page; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.support.FindBy; | |
import org.openqa.selenium.support.How; | |
import org.openqa.selenium.support.PageFactory; | |
import org.openqa.selenium.support.ui.ExpectedConditions; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
import java.util.List; | |
/** | |
* Created by IEUser on 1/19/2017. | |
*/ | |
public class ShipwreckPage { | |
@FindBy(how = How.TAG_NAME, tagName = "H3") | |
private WebElement banner; | |
@FindBy(how = How.LINK_TEXT, linkText = "Add New Shipwreck") | |
private WebElement addShipWreckButton; | |
private WebElement name; | |
private WebElement description; | |
private WebElement condition; | |
private WebElement yearDiscovered; | |
private WebElement depth; | |
private WebElement latitude; | |
private WebElement longitude; | |
@FindBy(how = How.CSS, css = "input[value='Save']") | |
private WebElement saveButton; | |
@FindBy(how = How.CSS, css = "table.shipwrecktable tr") | |
List<WebElement> rows; | |
private WebDriver driver; | |
public ShipwreckPage(WebDriver driver) { | |
this.driver = driver; | |
PageFactory.initElements(driver, this); | |
new WebDriverWait(driver, 30) | |
.until(ExpectedConditions.elementToBeClickable(addShipWreckButton)); | |
} | |
public boolean isLoaded() { | |
return banner.getText().equals("All Shipwrecks"); | |
} | |
public ShipwreckPage addNewShipwreck(String name, String desc, | |
String condition, String discovered, | |
String depth, String lat, String lng) { | |
addShipWreckButton.click(); | |
this.name.sendKeys(name); | |
this.description.sendKeys(desc); | |
this.condition.sendKeys(condition); | |
this.yearDiscovered.sendKeys(discovered); | |
this.depth.sendKeys(depth); | |
this.latitude.sendKeys(lat); | |
this.longitude.sendKeys(lng); | |
saveButton.click(); | |
return new ShipwreckPage(driver); | |
} | |
public int recordCount() { | |
return rows.size(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.common; | |
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; | |
import org.apache.poi.ss.usermodel.*; | |
import org.apache.poi.xssf.usermodel.XSSFCell; | |
import org.apache.poi.xssf.usermodel.XSSFRow; | |
import org.apache.poi.xssf.usermodel.XSSFSheet; | |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
/** | |
* Created by IEUser on 1/19/2017. | |
*/ | |
public class Spreadsheet { | |
private transient String[][] data = null; | |
public Spreadsheet(final InputStream excelInputStream) | |
throws IOException, InvalidFormatException { | |
this.data = lodFromSpreadsheet(excelInputStream); | |
} | |
public String[][] getData() { | |
return data; | |
} | |
private String[][] lodFromSpreadsheet(final InputStream excelFile) | |
throws IOException, InvalidFormatException { | |
Workbook workbook = WorkbookFactory.create(excelFile); | |
Sheet sheet = workbook.getSheetAt(0); | |
int numberOfRows = countNonEmptyRows(sheet); | |
int numberOfColumns = countNonEmptyColumns(sheet); | |
String data[][] = new String[numberOfRows - 1][numberOfColumns]; | |
int rowNum = 0; | |
for (Row row : sheet) { | |
if (isEmpty(row)) { | |
break; | |
} else { | |
if (row.getRowNum() != 0) // Row 0 will be Header Row | |
{ | |
for (int column = 0; column < numberOfColumns; column++) { | |
Cell cell = row.getCell(column); | |
data[rowNum][column] = objectFrom(workbook, cell).toString(); | |
} | |
System.out.println(""); | |
rowNum++; | |
} | |
} | |
} | |
return data; | |
} | |
private boolean isEmpty(final Row row) { | |
Cell firstCell = row.getCell(0); | |
boolean rowIsEmpty = (firstCell == null) | |
|| (firstCell.getCellType() == Cell.CELL_TYPE_BLANK); | |
return rowIsEmpty; | |
} | |
/** | |
* Count the number of columns, using the number of non-empty cells in the | |
* first row. | |
*/ | |
private int countNonEmptyColumns(final Sheet sheet) { | |
Row firstRow = sheet.getRow(0); | |
return firstEmptyCellPosition(firstRow); | |
} | |
/** | |
* Count the number of rows | |
*/ | |
private int countNonEmptyRows(final Sheet sheet) { | |
int rowNum = 0; | |
for (Row row : sheet) { | |
if (isEmpty(row)) { | |
break; | |
} | |
rowNum++; | |
} | |
return rowNum; | |
} | |
private int firstEmptyCellPosition(final Row cells) { | |
int columnCount = 0; | |
for (Cell cell : cells) { | |
if (cell.getCellType() == Cell.CELL_TYPE_BLANK) { | |
break; | |
} | |
columnCount++; | |
} | |
return columnCount; | |
} | |
private Object objectFrom(final Workbook workbook, final Cell cell) { | |
Object cellValue = null; | |
if (cell.getCellType() == Cell.CELL_TYPE_STRING) { | |
cellValue = cell.getRichStringCellValue().getString(); | |
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { | |
cellValue = getNumericCellValue(cell); | |
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { | |
cellValue = cell.getBooleanCellValue(); | |
} else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { | |
cellValue = evaluateCellFormula(workbook, cell); | |
} | |
return cellValue; | |
} | |
private Object getNumericCellValue(final Cell cell) { | |
Object cellValue; | |
if (DateUtil.isCellDateFormatted(cell)) { | |
cellValue = new Date(cell.getDateCellValue().getTime()); | |
} else { | |
cellValue = cell.getNumericCellValue(); | |
} | |
return cellValue; | |
} | |
private Object evaluateCellFormula(final Workbook workbook, final Cell cell) { | |
FormulaEvaluator evaluator = workbook.getCreationHelper() | |
.createFormulaEvaluator(); | |
CellValue cellValue = evaluator.evaluate(cell); | |
Object result = null; | |
if (cellValue.getCellType() == Cell.CELL_TYPE_BOOLEAN) { | |
result = cellValue.getBooleanValue(); | |
} else if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) { | |
result = cellValue.getNumberValue(); | |
} else if (cellValue.getCellType() == Cell.CELL_TYPE_STRING) { | |
result = cellValue.getStringValue(); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment