Run your first Selenium test on webmate by creating an API key, selecting a browser environment, and sending the session to the webmate Selenium hub.
You will learn how to generate the API key required for webmate tests, deploy a device, and interact with the Selenium session.
Generating an API key We start this article in our General Profile Information view. To get there please look into this article
.
Click API key in the left sidebar.
Provide a name for the new API Key to identify it later. Then click Create New Key .
The new API Key will be shown to you.
Please make sure to copy it for use, as it will be shown only once. Do not store it in a location where someone else can access it.
General Setup Integrating existing Selenium tests with the webmate infrastructure requires replacing the WebDriver endpoint and adding webmate-specific WebDriver capabilities.
For the migration path, see Use Existing Tests with webmate
.
Use a current W3C-compatible Selenium client, such as Selenium 4. Selenium 3 is no longer supported for new webmate browser sessions.
The complete example
at the end of the page includes the full code in one block.
Setting up the browser First, define the login credentials for webmate, the project ID where the tests should run, and the Selenium URL for webmate.
1
2
3
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability ("wm:apikey" , "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=" );
caps.setCapability ("wm:project" , "7c4f09fd-xxx-xxx-xxx-xxx" );
1
2
3
4
capabilities : {
email : "xxx@xxx.com" ,
project : "7c4f09fd-xxx-xxx-xxx-xxx"
}
Specify the browser and standard browser capabilities to create the RemoteWebDriver and seleniumSession.
1
2
3
4
5
6
caps.setCapability ("browserName" , "CHROME" );
caps.setCapability ("browserVersion" , "126" );
caps.setCapability ("platformName" , "WINDOWS_11_64" );
RemoteWebDriver driver;
driver = new RemoteWebDriver(new URL("https://selenium.webmate.io/wd/hub" ), caps);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
options = {
capabilities : {
browserName : "CHROME" ,
version : "83" ,
platformName : "WINDOWS_11_64" ,
// login credentials from above
},
hostname : "app.webmate.io" ,
protocol : "https" ,
port : 44444 ,
connectionRetryTimeout : 240000 ,
connectionRetryCount : 1 ,
logLevel : 'debug'
};
const browser : BrowserObject = await webdriverio .remote (options );
The browserName can be any of the following: APPIUM, CHROME, FIREFOX, IE, EDGE, SAFARI, OPERA.
The platformName is a string of the following format: platformType_platformVersion_suffix.
The platformType can be any of the following: Android, iOS, WINDOWS, MACOS, UBUNTU.
platformVersion is the version of the operating system (for macOS this is the release name, not the version number. e.g. MACOS_Monterey_xx), and the suffix is for example 64 for an 64 bit OS.
Here you have to check similarly to the OS you deployed that the browser version is available on the device that was deployed in the devices menu of your project.
When a device is already deployed, click on Details and switch to machine.browsers to see a list of all browser version available on this device.
webmate installs required browsers dynamically for the selected environment where supported. Do not base new test setup on legacy browser templates; choose the browser, browser version, and platform through capabilities instead.
This small set of capabilities is enough to start a Selenium test with webmate. To organize information in TestLab, add more webmate-specific capabilities.
1
2
3
caps.setCapability ("wm:autoScreenshots" , true );
caps.setCapability ("wm:name" , "A sample selenium test" );
caps.setCapability ("wm:tags" , "Hello World, tag2" );
1
2
3
"wm:autoScreenshots" : true ,
"wm:name" : "A sample selenium test" ,
"wm:tags" : "Hello World, tag2"
The first enables the test to automatically take screenshots when the view changes, so you can take a look at the test in the test lab and see exactly what the browser did.
To group the tests, you can name them and provide tags to filter them later.
For tags and test names, see Organize webmate Tests
.
Interacting with the browser You can visit webpages by calling
1
driver.get ("http://www.examplepage.org/" );
1
browser .url ("http://www.examplepage.org/" );
All code Use the complete code below as a copyable starting point.
The example is embedded directly from the
webmate-sdk-samples (opens in a new tab)
and webmate-sdk-js-samples (opens in a new tab)
repositories, so what you see here is exactly what compiles and runs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package examples;
import com.testfabrik.webmate.javasdk.*;
import com.testfabrik.webmate.javasdk.browsersession.*;
import com.testfabrik.webmate.javasdk.selenium.WebmateSeleniumSession;
import com.testfabrik.webmate.javasdk.testmgmt.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import static examples.MyCredentials.*;
import static examples.helpers.Helpers.waitForElement;
import static org.junit.Assert.assertEquals;
/**
* Simple test showing how to perform a Selenium test using webmate.
*/
@RunWith (JUnit4.class )
public class SeleniumTest {
private WebmateAPISession webmateSession;
@Before
public void setup () throws URISyntaxException {
WebmateAuthInfo authInfo = new WebmateAuthInfo(MyCredentials.WEBMATE_APIKEY );
webmateSession = new WebmateAPISession(
authInfo,
WebmateEnvironment.create (new URI(WEBMATE_API_URI)),
WEBMATE_PROJECTID);
}
@Test
public void performTest () throws MalformedURLException {
Platform platform = new Platform(PlatformType.WINDOWS , "11" , "64" );
Browser browser = new Browser(BrowserType.FIREFOX , "106" , platform);
executeTestInBrowser(browser);
}
private DesiredCapabilities getCapabilities (Browser browser) {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability ("browserName" , browser.getBrowserType ().getValue ());
caps.setCapability ("version" , browser.getVersion ());
caps.setCapability ("platform" , browser.getPlatform ().toString ());
caps.setCapability (WebmateCapabilityType.API_KEY , WEBMATE_APIKEY);
caps.setCapability (WebmateCapabilityType.PROJECT , WEBMATE_PROJECTID.toString ());
// See com.testfabrik.webmate.javasdk.WebmateCapabilityType for webmate specific capabilities
// caps.setCapability("wm:autoScreenshots", true);
caps.setCapability ("wm:name" , "A sample selenium test" );
caps.setCapability ("wm:tags" , "Sprint=34, Hello World" );
return caps;
}
public void executeTestInBrowser (Browser browser) throws MalformedURLException {
System.out .println ("Starting test for " + browser.getBrowserType () + " " + browser.getVersion () + " on " + browser.getPlatform ());
DesiredCapabilities caps = getCapabilities(browser);
RemoteWebDriver driver = new RemoteWebDriver(new URL(WEBMATE_SELENIUM_URL), caps);
WebmateSeleniumSession seleniumSession = webmateSession.addSeleniumSession (driver.getSessionId ().toString ());
BrowserSessionRef browserSession = webmateSession.browserSession
.getBrowserSessionForSeleniumSession (driver.getSessionId ().toString ());
try {
driver.get ("http://www.examplepage.org/version/future/" );
browserSession.createState ("start" );
System.out .println ("Clicking on something that will redirect us..." );
waitForElement(driver, "goto-examplepage" ).click ();
assertEquals("Cross Browser Issues Example" , driver.getTitle ());
driver.get ("http://www.examplepage.org/form_interaction" );
System.out .println ("Click on link" );
waitForElement(driver, "lk" ).click ();
assertEquals("Link Clicked!" , waitForElement(driver, ".success" ).getText ());
browserSession.createState ("after link" );
browserSession.startAction ("Click on button" );
System.out .println ("Clicking on Button" );
waitForElement(driver, "bn" ).click ();
browserSession.finishAction ();
browserSession.startAction ("Click on Checkbox" );
System.out .println ("Clicking on Checkbox" );
waitForElement(driver, "ck" ).click ();
browserSession.finishAction ();
browserSession.startAction ("Click on Radiobutton" );
System.out .println ("Clicking on RadioButton" );
waitForElement(driver, "rd" ).click ();
browserSession.createState ("after radio button" );
browserSession.finishAction ("was successful" );
System.out .println ("Clicking on Element with a Hover Event" );
waitForElement(driver, "mover" ).click ();
System.out .println ("Entering some Text..." );
waitForElement(driver, "text-input" ).click ();
waitForElement(driver, "text-input" ).sendKeys ("Test test" );
System.out .println ("Entering more text..." );
waitForElement(driver, "area" ).click ();
waitForElement(driver, "area" ).sendKeys ("Here some more test" );
seleniumSession.finishTestRun (TestRunEvaluationStatus.PASSED , "TestRun completed successfully" );
System.out .println ("Selenium expedition completed" );
} catch (Throwable e) {
seleniumSession.finishTestRun (TestRunEvaluationStatus.FAILED , "TestRun has failed" );
e.printStackTrace ();
} finally {
driver.quit ();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as webdriverio from 'webdriverio' ;
import logger from '@wdio/logger' ;
import 'mocha' ;
import {should } from 'chai' ;
import * as Webmate from "webmate-sdk-js" ;
import {Browser , BrowserType , Platform , PlatformType , TestRunEvaluationStatus , WebmateAPISession } from "webmate-sdk-js" ;
import {
MY_WEBMATE_APIKEY ,
MY_WEBMATE_PROJECTID ,
WEBMATE_API_URL ,
WEBMATE_SELENIUM_HOST ,
WEBMATE_SELENIUM_PORT ,
WEBMATE_SELENIUM_PROTOCOL
} from "./credentials" ;
import BrowserObject = WebdriverIO .BrowserObject ;
should ();
describe ('Selenium Demo Test Suite' , function () {
let log = logger ('tests:selenium' );
let webmateSession : WebmateAPISession ;
// time out for test execution
this .timeout (600000 );
before (function () {
// The user data can be configured in credentials.ts
webmateSession = Webmate .startSession (MY_WEBMATE_APIKEY , WEBMATE_API_URL , MY_WEBMATE_PROJECTID );
});
it ('should execute a simple selenium test' , async () => {
let errorHandler = function (e ) {
log .error (`An error occurred ${ e } ` );
throw e ;
};
let platform = new Platform (PlatformType .WINDOWS , "11" , "64" );
let browser = new Browser (BrowserType .CHROME , "106" , platform );
await executeTestInBrowser (browser ).catch (errorHandler );
});
function getOptions (browser : Browser ): WebdriverIO .RemoteOptions {
return {
capabilities : {
browserName : browser.browserType ,
version : browser.version ,
platform : browser.platform.toString (),
// @ts-ignore
apikey : MY_WEBMATE_APIKEY ,
project : MY_WEBMATE_PROJECTID
},
hostname : WEBMATE_SELENIUM_HOST ,
protocol : WEBMATE_SELENIUM_PROTOCOL ,
port : WEBMATE_SELENIUM_PORT ,
connectionRetryTimeout : 240000 ,
connectionRetryCount : 1 ,
logLevel : 'debug'
};
}
async function executeTestInBrowser (browser : Browser ) {
log .info (`Starting test for ${ browser .browserType } ${ browser .version } on ${ browser .platform .toString ()} ` );
let options = getOptions (browser );
const browserObj : BrowserObject = await webdriverio .remote (options );
let urlExamplePageFuture : string = "http://www.examplepage.org/version/future" ;
log .info ("Navigating to " , urlExamplePageFuture );
await browserObj .url (urlExamplePageFuture );
let seleniumSession = webmateSession .addSeleniumSession (browserObj .sessionId );
try {
await webmateSession .browserSession .createState ("initial state" ).toPromise ();
log .info ("Clicking on something that will redirect us..." );
let navlink = await browserObj .$ ("#goto-examplepage" );
await navlink .waitForExist ();
await navlink .click ();
let title = await browserObj .getTitle ();
title .should .eq ("Cross Browser Issues Example" , "Page title is incorrect" );
let urlExamplePageFormInteraction = "http://www.examplepage.org/form_interaction" ;
log .info ("Navigate to " , urlExamplePageFormInteraction );
await browserObj .url (urlExamplePageFormInteraction );
log .info ("Click on link" );
let link = await browserObj .$ ("#lk" );
await link .click ();
let successBox = await browserObj .$ (".success" );
await successBox .isExisting ();
let sucText = await successBox .getText ();
sucText .should .equal ("Link Clicked!" , "Success text was wrong" );
await webmateSession .browserSession .createState ("after link" ).toPromise ();
await webmateSession .browserSession .startAction ("Click on button" ).toPromise ();
log .info ("Clicking button" );
let bn = await browserObj .$ ("#bn" );
await bn .click ();
await webmateSession .browserSession .finishAction ().toPromise ();
await webmateSession .browserSession .startAction ("Click on checkbox" ).toPromise ();
log .info ("Clicking on checkbox" );
let ck = await browserObj .$ ("#ck" );
await ck .click ();
await webmateSession .browserSession .finishAction ().toPromise ();
await webmateSession .browserSession .startAction ("Click on radio button" ).toPromise ();
log .info ("Clicking radio button" );
let rd = await browserObj .$ ("#rd" );
await rd .click ();
await webmateSession .browserSession .createState ("after radio button" ).toPromise ();
await webmateSession .browserSession .finishAction ("was successful" ).toPromise ();
log .info ("Clicking on element with hover event" );
let mover = await browserObj .$ ("#mover" );
await mover .click ();
log .info ("Entering some text..." );
let input = await browserObj .$ ("#text-input" );
await input .click ();
await input .setValue ("Test test" );
log .info ("Entering some more text..." );
let area = await browserObj .$ ("#area" );
await area .click ();
await area .setValue ("Here some more test" );
await seleniumSession .finishTestRun (TestRunEvaluationStatus .PASSED , "TestRun completed successfully" ).toPromise ();
log .info ("Selenium expedition completed" );
} catch (err ) {
await seleniumSession .finishTestRun (TestRunEvaluationStatus .FAILED , "TestRun has failed" ).toPromise ();
// Rethrow the error
throw err ;
} finally {
await browserObj .deleteSession ();
}
return browserObj .sessionId ;
}
});
Next steps This is the basic Selenium setup for the webmate Selenium hub. The webmate SDK provides more features for automated and manual workflows.
For SDK workflows, see Using the SDK
.