Skip to content

Instantly share code, notes, and snippets.

@racinmat
Last active February 20, 2018 14:22
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 racinmat/22c34ed9d1a93563f42c0869e59e20f3 to your computer and use it in GitHub Desktop.
Save racinmat/22c34ed9d1a93563f42c0869e59e20f3 to your computer and use it in GitHub Desktop.

Firefox:

  1. stahnout GeckoDriver zde https://github.com/mozilla/geckodriver/releases
  2. Poté, co jsou přidané dependencies, nahraďte verzi selenia: - z <version>2.53.0</version> - na <version>3.9.1</version>

Chrome:

  1. stahnout ChromeDriver zde https://sites.google.com/a/chromium.org/chromedriver/downloads

Netbeans:

  1. založit v netbeans MAVEN project
  2. přidat do Test packages třídu TestProstredi.java
    • pokud tam Test packages nejsou, tak:
    • pravým tlačítkem kliknout na projekt
    • new -> other -> Unit tests, Junit
  3. otevřít POM - pravým tlačítkem kliknout na project -> open POM
  4. přidat do pom.xml ten kus xml, nad
<?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">
  tady jsou nějaké další věci
  
  TODO: sem přidat dependencies
</project>
  1. přidat gecko driver do cesty
  2. spustit testy: pravým tlačítkem kliknout na projekt -> Test

IntellijIdea:

  1. založit MAVEN project
  2. přidat do src/test/java třídu TestProstredi.java
  3. upravit pom.xml, enable auto-import
  4. pravý tlačítkem kliknout na projekt, Run all tests
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
package rename_me;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.junit.Assert.assertTrue;
//Be careful in splitting the test code to individual @Tests:
//an execution plan of Tests is not guaranteed by default
//We need to use @FixMethodOrder(MethodSorters.{NAME_ASCENDING}) above unit test class declaration
//http://junit.org/apidocs/index.html?org/junit/runners/MethodSorters.html
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestProstredi {
static RemoteWebDriver driver;
@BeforeClass
public static void beforeClass() {
// tohle je pro chrome
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
// tohle je pro firefox
System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe");
driver = new FirefoxDriver();
}
@AfterClass
public static void afterClass() {
//Pause.pause(2);
driver.quit();
}
@Test
public void step1_loginTest() {
driver.get("http://demo.redmine.org");
driver.findElement(By.linkText("Sign in")).click();
waitForElement(driver,By.id("username"));
driver.findElement(By.id("username")).sendKeys("testuser1358");
driver.findElement(By.id("password")).sendKeys("testit135");
driver.findElement(By.name("login")).click();
}
@Test
public void step2_projectTest() {
// FIND TESTING PROJECT
driver.findElement(By.linkText("Projects")).click();
driver.findElement(By.linkText("TestingProjectTraining")).click();
// CHECK NUMBER OF BUGS
WebElement bugsLinkElement = driver.findElement(By.linkText("Bug"));
WebElement bugsStatsElement = bugsLinkElement.findElement(By.xpath("parent::node()"));
String bugsStatusString=bugsStatsElement.getText();
String totalBugsCount=bugsStatusString.substring(bugsStatusString.indexOf("/")+1).trim();
int totalBugsCountNumber = (new Integer(totalBugsCount)).intValue();
// JUnit style assert
assertTrue("Log message: totalBugsCountNumber is less than 20",totalBugsCountNumber>20);
//fail();
}
// try to set alwaysRun false and fail previous test
//@Test (dependsOnMethods={"projectTest"})
@Test
public void step3_logoutTest() {
// LOG OUT
//Log.log("executing logout");
driver.findElement(By.linkText("Sign out")).click();
}
private void waitForElement(RemoteWebDriver driver, final By by){
Wait<WebDriver> wait = new WebDriverWait(driver, 10);
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return driver.findElement(by).isDisplayed();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment