lundi 21 octobre 2019

Java TestNG & Docker Desktop: Start & Stop Docker Programmatically Java Coding Question (Mac)

I'm currently learning using docker desktop with Java and a TestNG framework. I'm finding docker very straight forward however, I've now have a problem with running docker programmatically as a Mac user. After a bit of research someone posted the following code on my course but gave zero explanations except that it works (but I don't know how to run this code).

However, I have so many questions and I really need your collective expert answers (plus I think this will help anyone else in the future).

I've done a video here. Please click

Questions (scroll down for the code)

1. public class ProcessUtils class.

Will this allow me to stop and and start the docker container programatically?

Seems like a silly questions but i just want to be 10000% sure.

2. User of "user.dir" in TestBase Class

This appears a number of times here:

private static final String SETUP_DOCKER_SCRIPT_FILE = System.getProperty("user.dir") + "/dockerup.sh";
private static final String SCALE_CHROME_FILE = System.getProperty("user.dir") + "/docker_scale.sh";
private static final String TEAR_DOWN_SCRIPT_FILE = System.getProperty("user.dir") + "/dockerdown.sh";
private static final String EXPECTED_MESSAGE = "The node is registered to the hub 
and ready to use";

I am assuming its a directory. If so:

**What directory (ies) do you mean for each of these variables?*

Do you mean the directory location of the process Utils class? ( I realise that is not the case now post video but please confirm)

Do you also mean the location of docker desktop? *Do you mean some other directory?**

3. Test Base Class:

import static utils.ProcessUtils.execute;

Was this just a simple case of importing the from the Utils package into the test package? Just want to be sure?

4. In the test class public class chrome3 extends TestBase

Did you just do a simple inheritance from the TestBase file?

It looks like you did

5. Would I run this test as a normal Maven TestNG suite?

I hope my questions and my video are clear (also I said in the video there was uses of inheritance but was only 1 instance and 1 import. My bad).

Code1. Start and Stop docker programmatically

package utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ProcessUtils {

    public static void execute(String path, String waitForExpectedMessage) {
        execute(path, waitForExpectedMessage, false);
    }

    public static void execute(String path, boolean waitForCompletion) {
        execute(path, null, waitForCompletion);
    }

    private static void execute(String path, String waitForExpectedMessage, boolean waitForCompletion) {

        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(path);

            if (waitForCompletion) {
                proc.waitFor();
            }

            StringBuffer output = new StringBuffer();
            BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = "";
            while ((line = reader.readLine())!= null
                    && (waitForExpectedMessage != null && !line.contains(waitForExpectedMessage))) {
                output.append(line + "\n");
            }

            output.append(line + "\n");

            // To Debug
            if (line != null) {
                System.out.println("### " + output);
            }

        } catch (Throwable t) {
            t.printStackTrace();
        }

    }


}

Part 2: Set up and Close for Automated testing with TestNG annotations

package tests;

import static utils.ProcessUtils.execute;

import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;


public class TestBase {

    private static final String SETUP_DOCKER_SCRIPT_FILE = System.getProperty("user.dir") + "/dockerup.sh";
    private static final String SCALE_CHROME_FILE = System.getProperty("user.dir") + "/docker_scale.sh";
    private static final String TEAR_DOWN_SCRIPT_FILE = System.getProperty("user.dir") + "/dockerdown.sh";
    private static final String EXPECTED_MESSAGE = "The node is registered to the hub and ready to use";
    public static ThreadLocal<RemoteWebDriver> remoteWebDriver = new ThreadLocal<>();

    @BeforeSuite
    public void setUp() {
        execute(SETUP_DOCKER_SCRIPT_FILE, EXPECTED_MESSAGE);
        execute(SCALE_CHROME_FILE, true);
    }

    @BeforeClass
    public void beforeTest() throws Exception {
        URL url = new URL("http://localhost:4444/wd/hub");
        DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
        remoteWebDriver.set(new RemoteWebDriver(url, desiredCapabilities));
    }

    @AfterClass
    public void tearDown() {
        remoteWebDriver.get().close();
    }

    @AfterSuite
    public void afterTest() {
        execute(TEAR_DOWN_SCRIPT_FILE, true);
    }

}
  1. Test Execution

    public class chrome3 extends TestBase {

    @Test
    public void remoteWebdriverTest() {
        remoteWebDriver.get().get("http://google.co.uk");
        System.out.println(remoteWebDriver.get().getTitle());
    
    }
    

Aucun commentaire:

Enregistrer un commentaire