TestNG Automated Testing
See our TestNG example repository for a simple example on how to run TestNG tests on TestingBot.
TestNG is a framework similar to JUnit and NUnit, which supports some additional commands and features.
You can find more info about TestNG on the TestNG website.
To run your first test with TestNG, please follow the simple example below.
Example code
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class TestingBotTest {
private WebDriver driver;
@BeforeClass
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "chrome");
capabilities.setCapability(CapabilityType.BROWSER_VERSION, "latest");
capabilities.setCapability(CapabilityType.PLATFORM_NAME, "WIN10");
Map<String, Object> testingBotOptions = new HashMap<>();
testingBotOptions.put("name", "Testing Selenium");
capabilities.setCapability("tb:options", testingBotOptions);
driver = new RemoteWebDriver(
new URL("https://api_key:api_secret@hub.testingbot.com/wd/hub"), capabilities);
}
@Test
public void testSimple() throws Exception {
driver.get("https://testingbot.com/");
String searchHeader = driver.findElement(By.cssSelector("H1"))
.getText().toLowerCase();
Assert.assertTrue(searchHeader.contains("automated"));
}
@AfterClass
public void tearDown() throws Exception {
driver.quit();
}
}
Configuring capabilities
To run your existing tests on TestingBot, your tests will need to be configured to use the TestingBot remote machines. If the test was running on your local machine or network, you can simply change your existing test like this:
Before: After:Specify Browsers & Devices
To let TestingBot know on which browser/platform/device you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.
Testing Internal Websites
We've built TestingBot Tunnel, to provide you with a secure way to run tests against your staged/internal webapps.
Please see our TestingBot Tunnel documentation for more information about this easy to use tunneling solution.
The example below shows how to easily run a TestNG test with our Tunnel:
1. Download our tunnel and start the tunnel:
2. Adjust your test: instead of pointing to 'hub.testingbot.com/wd/hub'
like the example above - change it to point to your tunnel's IP address.
Assuming you run the tunnel on the same machine you run your tests, change to 'localhost:4445/wd/hub'
. localhost is the machine running the tunnel, 4445 is the default port of the tunnel.
This way your test will go securely through the tunnel to TestingBot and back:
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class TestingBotTest {
private WebDriver driver;
@BeforeClass
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "chrome");
capabilities.setCapability(CapabilityType.BROWSER_VERSION, "latest");
capabilities.setCapability(CapabilityType.PLATFORM_NAME, "WIN10");
Map<String, Object> testingBotOptions = new HashMap<>();
testingBotOptions.put("name", "Testing Selenium");
capabilities.setCapability("tb:options", testingBotOptions);
driver = new RemoteWebDriver(
new URL("http://api_key:api_secret@localhost:4445/wd/hub"), capabilities);
}
@Test
public void testSimple() throws Exception {
driver.get("https://testingbot.com/");
String searchHeader = driver.findElement(By.cssSelector("H1"))
.getText().toLowerCase();
Assert.assertTrue(searchHeader.contains("automated"));
}
@AfterClass
public void tearDown() throws Exception {
driver.getSessionId()
driver.quit();
}
}
Run tests in Parallel
Parallel Testing means running the same test, or multiple tests, simultaneously. This greatly reduces your total testing time.
You can run the same tests on all different browser configurations or run different tests all on the same browser configuration.
TestingBot has a large grid of machines and browsers, which means you can use our service to do efficient parallel testing. It is one of the key features we provide to greatly cut down on your total testing time.
To run a test on different browsers at the same time, you will need to create a testng.xml
file and create a testcase that uses parameters (org.testng.annotations.Parameters
). Please see the example below:
package tb;
import org.testng.annotations.Test;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.Assert;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class TestCase {
private WebDriver driver;
@BeforeClass
@org.testng.annotations.Parameters(value={"browser","version","platform"})
public void setUp(String browser, String version, String platform) throws Exception {
DesiredCapabilities capability = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, browser);
capabilities.setCapability(CapabilityType.BROWSER_VERSION, version);
capabilities.setCapability(CapabilityType.PLATFORM_NAME, platform);
Map<String, Object> testingBotOptions = new HashMap<>();
testingBotOptions.put("name", "Testing Selenium");
capabilities.setCapability("tb:options", testingBotOptions);
driver = new RemoteWebDriver(
new URL("https://api_key:api_secret@hub.testingbot.com/wd/hub"),
capability);
}
@Test
public void testSimple() throws Exception {
driver.get("https://www.google.com");
System.out.println("Page title is: " + driver.getTitle());
Assert.assertEquals("Google", driver.getTitle());
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("TestingBot");
element.submit();
driver = new Augmenter().augment(driver);
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(srcFile, new File("Screenshot.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
@AfterClass
public void tearDown() throws Exception {
driver.quit();
}
}
Now you need to create a testng.xml
file which will provide the test cases with parameters (browser combinations):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="3" name="Suite" parallel="tests">
<test name="FirstTest">
<parameter name="browser" value="firefox"/>
<parameter name="version" value="latest"/>
<parameter name="platform" value="MAC"/>
<classes>
<class name="tb.TestCase"/>
</classes>
</test> <!-- Test -->
<test name="SecondTest">
<parameter name="browser" value="chrome"/>
<parameter name="version" value="latest"/>
<parameter name="platform" value="WINDOWS"/>
<classes>
<class name="tb.TestCase"/>
</classes>
</test> <!-- Test -->
<test name="ThirdTest">
<parameter name="browser" value="safari"/>
<parameter name="version" value="latest"/>
<parameter name="platform" value="MAC"/>
<classes>
<class name="tb.TestCase"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Queuing
Every plan we provide comes with a limit of parallel tests.
If you exceed the number of parallel tests assigned to your account, TestingBot will queue the additional tests (for up to 6 minutes) and run the tests as soon as slots become available.
Mark tests as passed/failed
To see if a test passed or not in our member area, or to send additional meta-data to TestingBot, you need to use our API.
TestingBot has a Java client for using the TestingBot API.
Once included with your tests, you can send back test status and other meta-data to TestingBot:
import com.testingbot.testingbotrest.TestingbotREST;
@After
public void tearDown() throws Exception {
TestingbotREST r = new TestingbotREST("key", "secret");
Map<String, String> data = new HashMap<String, String>();
data.put("success", "1");
data.put("name", "My Test");
r.updateTest(driver.getSessionId(), data);
driver.quit();
}
More TestNG resources
Looking for more information regarding TestNG?
See our TestNG with Selenium article for more information on what TestNG supports, including assertions and annotations.
Other Java Framework examples
-
JUnit
JUnit is a unit testing framework for the Java programming language.
-
Parallel JUnit
By running multiple JUnit tests at the same time you can cut down on overall test time.
-
Selenide
Run tests with TestNG and Selenide.
-
TestNG + Cucumber
Run tests with TestNG and BDD Cucumber.