Home > Mobile >  Import greyed out in Java
Import greyed out in Java

Time:01-06

I've just started using Java today and have created some tests but I want to use JavascriptExecutor to be able to report tests passing or failing to Sauce Labs. I've imported the library I believe I need but it doesn't recognise the import either way. I believe I am doing it correctly but evidently I'm not and would like some help to understand where I'm going wrong.

My code looks like this:

package tests;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import pageobjects.Login;
import org.junit.jupiter.api.extension.TestWatcher;
import org.openqa.selenium.JavascriptExecutor;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class TestLogin extends BaseTest {
    private Login login;

    @RegisterExtension
    public SauceTestWatcher watcher = new SauceTestWatcher();

    @Before
    public void setUp() {
        login = new Login(driver);
    }

    @Test
    public void succeeded() {
        login.with("tomsmith", "SuperSecretPassword!");
        assertTrue("success message not present",
                login.successMessagePresent());
    }
    @Test
    public void failed() {
        login.with("tomsmith", "bad password");
        assertTrue("failure message wasn't present after providing bogus credentials",
                login.failureMessagePresent());
    }

    @Test
    public void failed2() {
        login.with("tomsmith", "bad password");
        assertFalse("success message was present after providing bogus credentials",
                login.successMessagePresent());
    }
    public class SauceTestWatcher implements TestWatcher {
        @Override
        public void testSuccessful(ExtensionContext context) {
            driver.executeScript("sauce:job-result=passed");
            driver.quit();
        }
        @Override
        public void testFailed(ExtensionContext context, Throwable cause) {
            driver.executeScript("sauce:job-result=failed");
            driver.quit();
        }
    }
}

I use import org.openqa.selenium.JavascriptExecutor; to get and I'm referencing it at the bottom: driver.executeScript("sauce:job-result=failed"); or the passed just above it. error - resolve method

CodePudding user response:

Invalidating cache and restarting resolved. Also changed the code slightly.

((JavascriptExecutor) driver).executeScript("sauce:job-result=passed");

This resolved my issue

  • Related