Muco
Muco

Reputation: 53

Selenium only for testing?

I'm just startet developing apps for android. I wrote some code, with logs into a website(which uses tons of JavaScript) and grabs some data, which i need. I used for that HtmlUnit. Now i see, that HtmlUnit doesn't work on android, so i need an alternative. I did something like this...

final WebClient webClient = new WebClient(BrowserVersion.EDGE);
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);

    final HtmlPage loginSite = webClient.getPage("...");

    final HtmlForm login_form = (HtmlForm) loginSite.getElementById("login_form");

    final HtmlSubmitInput button = (HtmlSubmitInput) login_form.getByXPath("//input[@id='login_submit_button']").get(0);

    final HtmlTextInput id = login_form.getInputByName("user");
    id.setValueAttribute("...");

    final HtmlPasswordInput pw = login_form.getInputByName("password");
    pw.setValueAttribute("...");

    button.click();

    webClient.waitForBackgroundJavaScript(2000);

    HtmlPage worldSelectionForm = (HtmlPage) webClient.getTopLevelWindows().get(0).getEnclosedPage();

    final List<HtmlAnchor> anchorlist = worldSelectionForm.getAnchors();

    getWorld(anchorlist, 126).click();
    webClient.waitForBackgroundJavaScript(5000);

    HtmlPage game = (HtmlPage) webClient.getTopLevelWindows().get(0).getEnclosedPage();
    System.out.print(game.asXml());


 public static HtmlAnchor getWorld(List<HtmlAnchor> anchorlist, int world) {
        HtmlAnchor anchor = null;
        for (HtmlAnchor a : anchorlist) {
            if (a.toString().contains("server_de" + String.valueOf(world))) {
                anchor = a;
            }
        }
        return anchor;
    }

I searched alot, but didn't find the answer to my question.

Can i do this too with Selenium? Is selenium only for Testing?

Greetings

Upvotes: 3

Views: 1611

Answers (2)

Andrew Regan
Andrew Regan

Reputation: 5113

The crucial decision with Selenium (WebDriver) is not whether or not you're "testing", but whether you need interactivity: whether you need to simulate a human being using a real browser.

If you need interactivity, one of the many, many WebDriver implementations (e.g. Selendroid) will be what you need.

However, if you don't, if you just need to grab content from a website, just use an HTTP library. That will be quicker, more resilient, and will avoid browser or device-specific issues. They can handle cookies, effect logins, and with all the power of HTTP behind them, but without the weight of a browser, they have a big advantage over the interactive tools.

For example, you can scrape and parse content very capably using JSoup (Android example).

Upvotes: 0

aneroid
aneroid

Reputation: 15962

For the question "Is selenium only for Testing?", the Selenium site starts with:

Selenium automates browsers. That's it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.

Check out Selendroid, which replaces the Android Driver.

They've got some good examples on their Quick Start page.

Upvotes: 2

Related Questions