org.framework.utils.TestUtility Maven / Gradle / Ivy
package org.framework.utils;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import org.framework.maincontroller.MainController;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
public class TestUtility extends MainController{
WebDriver driver;
public TestUtility(WebDriver driver)
{
this.driver = driver;
}
public void openPageInNewTab() throws AWTException {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
}
public void fileUpload(String fileLocation) throws AWTException {
StringSelection stringSelection = new StringSelection(fileLocation);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot;
robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
public void closeCurrentTab() {
driver.close();
}
public void closeAllTabsExceptFirst() {
ArrayList tabs = new ArrayList (driver.getWindowHandles());
for(int i=1;i al = new ArrayList(driver.getWindowHandles());
for(String window : al)
{
System.out.println(window);
}
}
public void hitEnter() throws AWTException {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
public void alertAccept() {
Alert alert = driver.switchTo().alert();
alert.accept();
}
public void selectByVisibleText(WebElement element, String text){
Select select = new Select(element);
select.selectByVisibleText(text);
}
public void selectByIndex(WebElement element, int index){
Select select = new Select(element);
select.selectByIndex(index);
}
public void mouseHoverActions(WebElement element)
{
Actions action = new Actions(driver);
action.moveToElement(element).build().perform();
}
public void clickAndHoldActions(WebElement element)
{
Actions action = new Actions(driver);
action.clickAndHold(element).build().perform();
}
public String getAlertText()
{
Alert alert = driver.switchTo().alert();
String alertText = alert.getText().trim();
return alertText;
}
public void alertDismiss() {
Alert alert = driver.switchTo().alert();
alert.dismiss();
}
public void verifyToolTip(WebElement element, String expectedToolTip) {
Assert.assertEquals(element.getAttribute("title").trim(),"Print this page");
}
public void maximizeScreen(WebDriver driver) {
java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point position = new Point(0, 0);
driver.manage().window().setPosition(position);
Dimension maximizedScreenSize =
new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
driver.manage().window().setSize(maximizedScreenSize);
}
public boolean assertAlertText(String expectedAlertText) throws Exception{
TestUtility utils = new TestUtility(driver);
boolean t = utils.getAlertText().replace("\n", "").trim().equals(expectedAlertText);
attachFile(utils.getAlertText().trim(), "actual alert text", "txt");
attachFile(expectedAlertText.trim(), "expected alert text", "txt");
utils.alertAccept();
return t;
}
}