Skip to content

get testwatcher code working #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test.java.com.saucelabs.advancedselenium;
package com.saucelabs.advancedselenium;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the way the tests is set up, based on this: https://github.com/saucelabs-training/advanced-selenium/tree/new_base repo, the tests won't work unless they are named as they were before


import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@
package test.java.com.saucelabs.advancedselenium;
package com.saucelabs.advancedselenium;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the way the tests is set up, based on this: https://github.com/saucelabs-training/advanced-selenium/tree/new_base repo, the tests won't work unless they are named as they were before


import com.saucelabs.saucebindings.SauceOptions;
import com.saucelabs.saucebindings.SauceSession;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.extension.TestWatcher;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.util.Collections

@ExtendWith(SauceTestBase.SauceTestWatcher.class) // added in 1.06

public class SauceTestBase {

RemoteWebDriver driver = null;
SauceSession session = null;

@RegisterExtension
public MyTestWatcher myTestWatcher = new MyTestWatcher();

@BeforeAll
public static void setExecution() {
// NOTE: This code is for convenience in this tutorial
// Normally this would be set in pom file or with IntelliJ
// Toggle this setting to determine whether tests run on SAUCE or LOCAL
System.setProperty("SELENIUM_PLATFORM", "LOCAL");
}

@BeforeEach
public void setUp(TestInfo testinfo) { // added testinfo parameters in 1.06
System.setProperty("SELENIUM_PLATFORM"="SAUCE");
ChromeOptions chromeOptions = new ChromeOptions();
if (System.getProperty("SELENIUM_PLATFORM") == null) {

if (System.getProperty("SELENIUM_PLATFORM").equals("LOCAL")) {
WebDriverManager.chromedriver().setup();

driver = new ChromeDriver(chromeOptions);
} else if (System.getProperty("SELENIUM_PLATFORM").equals("SAUCE")) {
SauceOptions sauceOptions = new SauceOptions(chromeOptions);
sauceOptions.setName(testinfo.getDisplayName()); // added in 1.06
SauceSession sauceSession = new SauceSession(sauceOptions);
driver = sauceSession.start();

session = new SauceSession(sauceOptions);
driver = session.start();
}
else {
throw new RuntimeException("You have no environment variable set that specifies the local or remote host");
throw new RuntimeException("Set System Property 'SELENIUM_PLATFORM' to determine where test is run");
}
}


@AfterEach
public void endSession() {
if (session != null) {
session.stop(true);
} else if (driver != null) {
driver.quit();
}
}

public class SauceTestWatcher implements TestWatcher { // entire class added in 1.06
private SauceSession session;

public void setSession(SauceSession session) {
this.session = session;
}

public class MyTestWatcher implements TestWatcher { // entire class added in 1.06
@Override
protected void testFailed(ExtensionContext context, Throwable cause) {
session.stop(false);
public void testFailed(ExtensionContext context, Throwable cause) {
if (session != null) {
session.stop(false);
} else {
System.out.println("Test Failed!");
driver.quit();
}
}

@Override
protected void testSuccessful(ExtensionContext context) {
session.stop(true);
public void testSuccessful(ExtensionContext context) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am gettting a log of 'Test Passed!' after test, but session info isn't being passed to Sauce Labs

if (session != null) {
session.stop(true);
} else {
System.out.println("Test Passed!");
driver.quit();
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test.java.com.saucelabs.advancedselenium;
package com.saucelabs.advancedselenium;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.jupiter.api.Test;
Expand Down