Skip to content

Instantly share code, notes, and snippets.

Created August 1, 2017 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9e0e843b17e8c789e8e6f92d5a848e5f to your computer and use it in GitHub Desktop.
Save anonymous/9e0e843b17e8c789e8e6f92d5a848e5f to your computer and use it in GitHub Desktop.
package com.denox.selenium;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class testeAlarme {
PhantomJSDriver wd; // Phantom
@Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs(); // Phantom
wd = new PhantomJSDriver(capabilities); // Phantom
wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testeAlarmeArmar() {
WebDriverWait wait = new WebDriverWait(wd, 30);
JavascriptExecutor executor = (JavascriptExecutor)wd;
wd.get("https://my-test.denox.com.br/#/");
try {
wd.findElement(By.id("username")).clear();
wd.findElement(By.id("username")).sendKeys("thamirissouza@denox.com.br");
wd.findElement(By.id("password")).clear();
wd.findElement(By.id("password")).sendKeys("a123456");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='login-block']//button[.='Entrar']")));
executor.executeScript("arguments[0].click();", wd.findElement(By.xpath("//div[@class='login-block']//button[.='Entrar']")));
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Started")));
executor.executeScript("arguments[0].click();", wd.findElement(By.linkText("Started")));
if(!(wd.findElement(By.cssSelector("div.card-dashboard-text")).getText().equals("Desarmado"))){
executor.executeScript("arguments[0].click();", wd.findElement(By.xpath("//a[@ng-click='vm.activateAlarmByDashboardCard(vm.getCurrentLocal().alarm)']")));
try { Thread.sleep(1000l); } catch (Exception e) { throw new RuntimeException(e); }
executor.executeScript("arguments[0].click();", wd.findElement(By.xpath("//button[contains(.,'Desarmar')]")));
}
try { Thread.sleep(10000l); } catch (Exception e) { throw new RuntimeException(e); }
assertEquals("Desarmado", wd.findElement(By.cssSelector("div.card-dashboard-text")).getText());
try { Thread.sleep(3000l); } catch (Exception e) { throw new RuntimeException(e); }
executor.executeScript("arguments[0].click();", wd.findElement(By.xpath("//a[@ng-click='vm.activateAlarmByDashboardCard(vm.getCurrentLocal().alarm)']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='modal-footer']//button[.='Armar']")));
executor.executeScript("arguments[0].click();", wd.findElement(By.xpath("//div[@class='modal-footer']//button[.='Armar']")));
try { Thread.sleep(3000l); } catch (Exception e) { throw new RuntimeException(e); }
if(wd.findElements(By.xpath("//div[@class='modal-dialog']")).size()>0){
System.out.println("Sensor magnético aberto ou equipamento ausente.");
executor.executeScript("arguments[0].click();", wd.findElement(By.xpath("//button[contains(.,'Sim')]")));
}
try { Thread.sleep(10000l); } catch (Exception e) { throw new RuntimeException(e); }
String txt = wd.findElement(By.cssSelector("div.card-dashboard-text")).getText();
if (txt.equals("Aguardando armar")){
for (int i = 0; i < 10; i++){
try { Thread.sleep(3000l); } catch (Exception e) { throw new RuntimeException(e); }
System.out.println("Esperando...");
txt = wd.findElement(By.cssSelector("div.card-dashboard-text")).getText();
if (!txt.equals("Aguardando armar"))
break;
}
}
if (txt.equalsIgnoreCase("Armado")||txt.equalsIgnoreCase("Vai disparar")||txt.equalsIgnoreCase("Disparado")){
assertNotEquals("Desarmado", wd.findElement(By.cssSelector("div.card-dashboard-text")).getText());
System.out.println("Alarme foi ativado");
}
else
fail("Falha! Alarme não foi ativado");
}
catch (Exception e) {
// Ignore technical errors during test execution
System.out.println("Erro! Teste não executado");
}
}
@Test
public void testeAlarmeDesarmar() {
WebDriverWait wait = new WebDriverWait(wd, 30);
JavascriptExecutor executor = (JavascriptExecutor)wd;
wd.get("https://my-test.denox.com.br/#/");
try {
wd.findElement(By.id("username")).clear();
wd.findElement(By.id("username")).sendKeys("thamirissouza@denox.com.br");
wd.findElement(By.id("password")).clear();
wd.findElement(By.id("password")).sendKeys("a123456");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='login-block']//button[.='Entrar']")));
executor.executeScript("arguments[0].click();", wd.findElement(By.xpath("//div[@class='login-block']//button[.='Entrar']")));
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Started")));
executor.executeScript("arguments[0].click();", wd.findElement(By.linkText("Started")));
try { Thread.sleep(5000l); } catch (Exception e) { throw new RuntimeException(e); }
String txt = wd.findElement(By.cssSelector("div.card-dashboard-text")).getText();
if (txt.equals("Aguardando armar")){
for (int i = 0; i < 10; i++){
try { Thread.sleep(2000l); } catch (Exception e) { throw new RuntimeException(e); }
System.out.println("Esperando...");
txt = wd.findElement(By.cssSelector("div.card-dashboard-text")).getText();
if (!txt.equals("Aguardando armar"))
break;
}
}
if (txt.equalsIgnoreCase("Armado")||txt.equalsIgnoreCase("Vai disparar")||txt.equalsIgnoreCase("Disparado")){
executor.executeScript("arguments[0].click();", wd.findElement(By.xpath("//a[@ng-click='vm.activateAlarmByDashboardCard(vm.getCurrentLocal().alarm)']")));
try { Thread.sleep(1000l); } catch (Exception e) { throw new RuntimeException(e); }
executor.executeScript("arguments[0].click();", wd.findElement(By.xpath("//button[contains(.,'Desarmar')]")));
try { Thread.sleep(5000l); } catch (Exception e) { throw new RuntimeException(e); }
txt = wd.findElement(By.cssSelector("div.card-dashboard-text")).getText();
if (txt.equals("Aguardando desarmar")){
for (int i = 0; i < 10; i++){
try { Thread.sleep(2000l); } catch (Exception e) { throw new RuntimeException(e); }
System.out.println("Esperando...");
txt = wd.findElement(By.cssSelector("div.card-dashboard-text")).getText();
if (!txt.equals("Aguardando desarmar"))
break;
}
}
assertEquals("Desarmado", wd.findElement(By.cssSelector("div.card-dashboard-text")).getText());
System.out.println("Alarme foi desativado");
}
else
fail("Falha! Alarme não está armado");
}
catch (Exception e) {
// Ignore technical errors during test execution
System.out.println("Erro! Teste não executado");
}
}
@After
public void tearDown() {
wd.quit();
}
public static boolean isAlertPresent(FirefoxDriver wd) {
try {
wd.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment