Я хочу наследовать удаленный веб-драйвер от моего класса BaseTest, чтобы все мои тесты в другом классе могли наследовать веб-драйвер. В настоящее время он реализован только во втором классе, я могу сделать это довольно легко, если я создаю тесты локально, но мы используем инструмент "CrossBrowserTesting" для масштабирования наших тестов. У кого-нибудь есть идеи, как это будет выглядеть синтаксически?
Мои попытки унаследовать его от класса BaseTest не увенчались успехом. Это не тот синтаксис, к которому я привык. Документация отличается от того, что я предоставил.
Class 1
public class BaseTest {
public static String CBUsername = ABCD1;
public static String CBAuthkey = HIJK1;
public static String OS = "Windows 10";
public static String Build = "3";
public static String Browser = "Chrome";
public static String BrowserVersion = "73x64";
public static String Resolution = "1366x768";
public static String RecordVideo = "True";
public static String RecordNetwork = "False";
}
Class 2
@Test
public void ExampleTest throws MalformedURLException, UnirestException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("name", "Dashboard"); // Set Name To Test Name
caps.setCapability("build", Build); // Set Build To Version Of Release
caps.setCapability("browserName", Browser); //Custom
caps.setCapability("version", BrowserVersion); //Custom
caps.setCapability("platform", OS); //Custom
caps.setCapability("screenResolution", Resolution); //Custom
caps.setCapability("record_video", RecordVideo); //Custom
caps.setCapability("record_network", RecordNetwork); //Custom
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://" + CBUsername + ":" + CBAuthkey +"@hub.crossbrowsertesting.com:80/wd/hub"), caps);
try {
/* Set testScore to fail in-case an error is discovered at runtime. */
myTest.testScore = "fail";
/*
* Enter Code Here
*
*/
/* if we get to this point, then all the assertions have passed. */
myTest.testScore = "pass";
}
catch(AssertionError ae) {
String snapshotHash = myTest.takeSnapshot((driver).getSessionId().toString());
myTest.setDescription((driver).getSessionId().toString(), snapshotHash, ae.toString());
myTest.testScore = "fail";
}
finally {
System.out.println("Test complete: " + myTest.testScore);
// here we make an api call to actually send the score
myTest.setScore((driver).getSessionId().toString(), myTest.testScore);
// and quit the driver
driver.quit();
}
}
public JsonNode setScore(String seleniumTestId, String score) throws UnirestException {
/* Mark a Selenium test as Pass/Fail */
String username = CBUsername; /* Your username */
String authkey = CBAuthkey; /* Your authkey */
HttpResponse<JsonNode> response = Unirest.put("http://crossbrowsertesting.com/api/v3/selenium/{seleniumTestId}")
.basicAuth(username, authkey)
.routeParam("seleniumTestId", seleniumTestId)
.field("action","set_score")
.field("score", score)
.asJson();
return response.getBody();
}
String takeSnapshot(String seleniumTestId) throws UnirestException {
/*
* Takes a snapshot of the screen for the specified test.
* The output of this function can be used as a parameter for setDescription()
*/
String username = CBUsername; /* Your username */
String authkey = CBAuthkey; /* Your authkey */
HttpResponse<JsonNode> response = Unirest.post("http://crossbrowsertesting.com/api/v3/selenium/{seleniumTestId}/snapshots")
.basicAuth(username, authkey)
.routeParam("seleniumTestId", seleniumTestId)
.asJson();
// grab out the snapshot "hash" from the response
String snapshotHash = (String) response.getBody().getObject().get("hash");
return snapshotHash;
}
public JsonNode setDescription(String seleniumTestId, String snapshotHash, String description) throws UnirestException{
/*
* sets the description for the given seleniemTestId and snapshotHash
*/
String username = CBUsername; /* Your username */
String authkey = CBAuthkey; /* Your authkey */
HttpResponse<JsonNode> response = Unirest.put("http://crossbrowsertesting.com/api/v3/selenium/{seleniumTestId}/snapshots/{snapshotHash}")
.basicAuth(username, authkey)
.routeParam("seleniumTestId", seleniumTestId)
.routeParam("snapshotHash", snapshotHash)
.field("description", description)
.asJson();
return response.getBody();
}
}
}