Example usage for org.openqa.selenium.remote Response Response

List of usage examples for org.openqa.selenium.remote Response Response

Introduction

In this page you can find the example usage for org.openqa.selenium.remote Response Response.

Prototype

public Response(SessionId sessionId) 

Source Link

Usage

From source file:com.seleniumtests.ut.uipage.htmlelements.TestHtmlElement.java

License:Apache License

@BeforeMethod(groups = { "ut" })
private void init() throws WebDriverException, IOException {
    // mimic sub elements of the HtmlElement
    List<WebElement> subElList = new ArrayList<WebElement>();
    subElList.add(subElement1);/*from  w  ww  .ja va2 s.  c  o  m*/
    subElList.add(subElement2);

    // list of elements correspond
    List<WebElement> elList = new ArrayList<WebElement>();
    elList.add(element);

    // add DriverExceptionListener to reproduce driver behavior
    eventDriver = spy(new CustomEventFiringWebDriver(driver).register(new DriverExceptionListener()));

    PowerMockito.mockStatic(WebUIDriver.class);
    when(WebUIDriver.getWebDriver()).thenReturn(eventDriver);
    when(driver.findElement(By.id("el"))).thenReturn(element);
    when(driver.findElements(By.name("subEl"))).thenReturn(subElList);
    when(driver.findElement(By.name("subEl"))).thenReturn(subElement1);
    when(driver.findElements(By.id("el"))).thenReturn(elList);
    when(driver.getKeyboard()).thenReturn(keyboard);
    when(driver.getMouse()).thenReturn(mouse);
    when(driver.switchTo()).thenReturn(locator);
    when(driver.executeScript(anyString())).thenReturn(Arrays.asList(100, 100));

    when(element.findElement(By.name("subEl"))).thenReturn(subElement1);
    when(element.findElements(By.name("subEl"))).thenReturn(subElList);
    when(element.getAttribute(anyString())).thenReturn("attribute");
    when(element.getSize()).thenReturn(new Dimension(10, 10));
    when(element.getLocation()).thenReturn(new Point(5, 5));
    when(element.getTagName()).thenReturn("h1");
    when(element.getText()).thenReturn("text");
    when(element.isDisplayed()).thenReturn(true);
    when(element.isEnabled()).thenReturn(true);

    when(subElement1.isDisplayed()).thenReturn(true);
    when(subElement2.isDisplayed()).thenReturn(true);
    when(subElement1.getLocation()).thenReturn(new Point(5, 5));
    when(subElement2.getLocation()).thenReturn(new Point(5, 5));

    when(mobileElement.getCenter()).thenReturn(new Point(2, 2));
    when(mobileElement.getLocation()).thenReturn(new Point(1, 1));
    when(mobileElement.isDisplayed()).thenReturn(true);
    when(mobileElement.getId()).thenReturn("12");

    // init for mobile tests
    AppiumCommandExecutor ce = Mockito.mock(AppiumCommandExecutor.class);
    Response response = new Response(new SessionId("1"));
    response.setValue(new HashMap<String, Object>());
    Response findResponse = new Response(new SessionId("1"));
    findResponse.setValue(mobileElement);

    when(ce.execute(any())).thenReturn(response, response, response, findResponse);
    doReturn(response).when(ce)
            .execute(argThat(command -> DriverCommand.NEW_SESSION.equals(command.getName())));
    doReturn(response).when(ce)
            .execute(argThat(command -> DriverCommand.FIND_ELEMENT.equals(command.getName())));
    doReturn(response).when(ce).execute(argThat(command -> "getSession".equals(command.getName())));

    // newSession, getSession, getSession, findElement

    mobileDriver = Mockito.spy(new AppiumDriver<>(ce, new DesiredCapabilities()));

    SeleniumTestsContextManager.getThreadContext().setTestType(TestType.WEB);
    SeleniumTestsContextManager.getThreadContext().setBrowser("htmlunit");
}

From source file:io.appium.java_client.remote.AppiumProtocolHandShake.java

License:Apache License

private Optional<Result> createSession(HttpClient client, JsonObject params) throws IOException {
    // Create the http request and send it
    HttpRequest request = new HttpRequest(HttpMethod.POST, "/session");
    String content = params.toString();
    byte[] data = content.getBytes(UTF_8);

    request.setHeader(CONTENT_LENGTH, String.valueOf(data.length));
    request.setHeader(CONTENT_TYPE, JSON_UTF_8.toString());
    request.setContent(data);//from   w  w w .j  a  v a2s .  c o m
    HttpResponse response = client.execute(request, true);

    Map<?, ?> jsonBlob = new HashMap<>();
    String resultString = response.getContentString();
    try {
        jsonBlob = new JsonToBeanConverter().convert(Map.class, resultString);
    } catch (ClassCastException e) {
        return Optional.empty();
    } catch (JsonException e) {
        // Fine. Handle that below
    }

    // If the result looks positive, return the result.
    Object sessionId = jsonBlob.get("sessionId");
    Object value = jsonBlob.get("value");
    Object w3cError = jsonBlob.get("error");
    Object ossStatus = jsonBlob.get("status");
    Map<String, ?> capabilities = null;
    if (value != null && value instanceof Map) {
        capabilities = (Map<String, ?>) value;
    } else if (value != null && value instanceof Capabilities) {
        capabilities = ((Capabilities) capabilities).asMap();
    }

    if (response.getStatus() == HttpURLConnection.HTTP_OK && sessionId != null && capabilities != null) {
        Dialect dialect = ossStatus == null ? Dialect.W3C : Dialect.OSS;
        return Optional.of(new Result(dialect, String.valueOf(sessionId), capabilities));
    }

    // If the result was an error that we believe has to do with the remote end failing to start the
    // session, create an exception and throw it.
    Response tempResponse = null;
    if ("session not created".equals(w3cError)) {
        tempResponse = new Response(null);
        tempResponse.setStatus(SESSION_NOT_CREATED);
        tempResponse.setValue(jsonBlob);
    } else if (ossStatus instanceof Number && ((Number) ossStatus).intValue() == SESSION_NOT_CREATED) {
        tempResponse = new Response(null);
        tempResponse.setStatus(SESSION_NOT_CREATED);
        tempResponse.setValue(jsonBlob);
    }

    if (tempResponse != null) {
        new ErrorHandler().throwIfResponseFailed(tempResponse, 0);
    }

    // Otherwise, just return empty.
    return Optional.empty();
}