com.github.stefaneicher.democd.HelloControllerUAT.java Source code

Java tutorial

Introduction

Here is the source code for com.github.stefaneicher.democd.HelloControllerUAT.java

Source

package com.github.stefaneicher.democd;

/*
 * #%L
 * user-acceptance-tests
 * %%
 * Copyright (C) 2015 Stefan Eicher
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import static org.junit.Assert.assertEquals;

public class HelloControllerUAT {

    public String url;
    private WebDriver driver;
    //    private URL base;

    //    @Test
    //    public void getHello() throws Exception {
    //        ResponseEntity<String> response = template.getForEntity(base.toString() + "greeting", String.class);
    //        assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));

    //    private RestTemplate template;

    @Before
    public void setUp() throws Exception {
        //        this.base = new URL("http://democd.cfapps.io/");
        //        template = new TestRestTemplate();
    }

    //    }

    @Before
    public void setup() throws Exception {
        runWith(souceLabDriver()).against("https://democd.scapp.io/index.html"); // todo read value from manifest.yml
        //        runWith(souceLabDriver()).against("http://localhost:8080/index.html");
        //        runWith(localChromeDriver()).against("https://democd.scapp.io/index.html");
        //        runWith(localChromeDriver()).against("http://localhost:8080/index.html");
    }

    private void against(String url) {
        this.url = url;
    }

    private HelloControllerUAT runWith(WebDriver driver) {
        this.driver = driver;
        return this;
    }

    @SuppressWarnings("unused")
    private WebDriver localChromeDriver() throws IOException {
        File chromeDriver = new File("src/test/resources/chromedriver_mac32");
        ChromeDriverService service = new ChromeDriverService.Builder().usingDriverExecutable(chromeDriver)
                .usingAnyFreePort().build();
        service.start();
        System.setProperty("webdriver.chrome.driver", String.valueOf(chromeDriver));
        return new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
    }

    private RemoteWebDriver souceLabDriver() throws MalformedURLException {
        String USERNAME = "stefaneicher";
        String ACCESS_KEY = "e6103003-5e27-4fae-b7a2-a400c32dc6ed";
        String URL = "http://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub";
        DesiredCapabilities caps = DesiredCapabilities.chrome();
        caps.setCapability("platform", "Windows XP");
        caps.setCapability("version", "43.0");
        return new RemoteWebDriver(new URL(URL), caps);
    }

    @After
    public void quitDriver() {
        driver.quit();
    }

    @Test
    public void testMainPage() throws InterruptedException {
        driver.get(url);
        WebElement element = driver.findElement(By.id("hello"));
        assertEquals("Hello democd", element.getText());
        // Check the title of the page
        assertEquals("democd", driver.getTitle());
    }
}