Search code examples
seleniumautomationappiumnativeroku

Will Roku WebDriver setup work with my Java framework


I know Roku Webdriver repository comes with python and Postman sample scripts but I was wondering if I could use my Java scripts - not javascript - to connect to go and automate. If so, does anyone have any examples how to setup the driver in Java?

https://developer.roku.com/en-ca/docs/developer-program/dev-tools/automated-channel-testing/web-driver.md


Solution

  • I figured it out. If anyone is interested in automating it in Java, this code will work as a starting point. Make sure to connect to Go server first. The steps are on https://developer.roku.com/en-ca/docs/developer-program/dev-tools/automated-channel-testing/automated-testing-overview.md . Dont forget to update bash or zshrc with

    export GOPATH=/Users/$USER/eclipse-workspace/automated-channel-testing-master

    Once the server is running, run the code. Make sure you add your own IP. Thats located in settings on the Roku device. Also add a close method so that you wont get any errors that the "session is already running". If you do get that, just reset the server.

    Again this is just a starting point - use the APIs in https://developer.roku.com/en-ca/docs/developer-program/dev-tools/automated-channel-testing/web-driver.md to really start automating.

    String ip ;
    JSONObject json;
    String cookie;
            
    public RokuDriver(String IP) {
        this.ip = IP;
        
        OkHttpClient client = new OkHttpClient().newBuilder()
                  .build(); 
                RequestBody body = RequestBody.create("{\n\t\"ip\": \""+ip+"\"\n}", MediaType.parse("application/json"));
                Request request = new Request.Builder()
                  .url("http://localhost:9000/v1/session")
                  .method("POST", body)
                  .addHeader("Content-Type", "application/json")
                  .build();
                try {
                    Response response = client.newCall(request).execute();
                    
                    JSONObject jsonBody = new 
                    JSONObject(response.body().string());                
                    cookie = jsonBody.getString("sessionId");                   
                    System.out.println(jsonBody.toString(4));               
                    response.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    close();
                }
    }
    
    public void down() {
        OkHttpClient client = new OkHttpClient().newBuilder()
                  .build();
                MediaType mediaType = MediaType.parse("application/json");
                RequestBody body = RequestBody.create("{\n\t\"button\": \"down\"\n}", MediaType.parse("application/json"));
                Request request = new Request.Builder()
                  .url("http://localhost:9000/v1/session/"+cookie+"/press")
                  .method("POST", body)
                  .addHeader("Content-Type", "application/json")
                  .build();
                try {
                    Response response = client.newCall(request).execute();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    

    POM:

    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.8.1</version>
    </dependency>
    
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp-urlconnection</artifactId>
        <version>4.8.1</version>
    </dependency>
    
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20200518</version>
    </dependency>
    
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>