Showing posts with label ie. Show all posts
Showing posts with label ie. Show all posts

Wednesday, February 20, 2013

[ Selenium 2.0 | WebDriver | Best practices ] How to check by URL that image exists


Purpose

Check that URL leeds to existent image. For example, when you need to check that image was really uploaded.
Actually, you could use such solution to check any URL and it's response.

Description

The Idea is to send an HTTP request by image-URL and check that response statuse code is OK (200)

Code sample

    // just look at your cookie's content (e.g. using browser) and import these settings from it
    private static final String SESSION_COOKIE_NAME = "JSESSIONID";
    private static final String DOMAIN = "domain.here.com";
    private static final String COOKIE_PATH = "/cookie/path/here";

    protected boolean isResourceAvailableByUrl(String resourceUrl) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        BasicCookieStore cookieStore = new BasicCookieStore();
        cookieStore.addCookie(getSessionCookie());
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        // resourceUrl - is url which leads to image
        HttpGet httpGet = new HttpGet(resourceUrl);

        try {
            HttpResponse httpResponse = httpClient.execute(httpGet, localContext);
            return httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
        } catch (IOException e) {
            return false;
        }
    }

    protected BasicClientCookie getSessionCookie() {
        Cookie originalCookie = webDriver.manage().getCookieNamed(SESSION_COOKIE_NAME);

        if (originalCookie == null) {
            return null;
        }


        String cookieName = originalCookie.getName();
        String cookieValue = originalCookie.getValue();
        BasicClientCookie resultCookie = new BasicClientCookie(cookieName, cookieValue);
        resultCookie.setDomain(DOMAIN);
        resultCookie.setExpiryDate(originalCookie.getExpiry());
        resultCookie.setPath(COOKIE_PATH);
        return resultCookie;
    }

Tuesday, February 19, 2013

[ Selenium 2.0 | WebDriver | Best practices ] Clear input element's value


Purpose

Clear value of HTML input element.
It should work successfully for browsers: Google Chrome, Firefox, IE, Safari.

Code sample


    protected void clearInput(WebElement webElement) {
        // isIE() - just checks is it IE or not - use your own implementation
        if (isIE() && "file".equals(webElement.getAttribute("type"))) {
            // workaround
            // if IE and input's type is file - do not try to clear it.
            // If you send:
            // - empty string - it will find file by empty path
            // - backspace char - it will process like a non-visible char
            // In both cases it will throw a bug.
            // 
            // Just replace it with new value when it is need to.
        } else {
            // if you have no StringUtils in project, check value still empty yet
            while (!StringUtils.isEmpty(webElement.getAttribute("value"))) {
                // "\u0008" - is backspace char
                webElement.sendKeys("\u0008");
            }
        }
    }


Notes

As for me, standard webElement.clear() doesn't work for all browsers. Commonly, it could not clear value and throws bug that element is not able to be clean:

Caused by: org.openqa.selenium.InvalidElementStateException: Element must not be hidden, disabled or read-only