Opening Selenium Webdriver tests in the same window - java

I have dozens of Selenium Webdriver tests. I want to run them all at once. How do I run the test so that each test does not open a new Webdriver browser window?

You have to initiate/teardown your webdriver in a #BeforeClass/#AfterClass, and use this webdriver in all your test.
public class MyTest {
WebDriver driver;
#BeforeClass
public static void setUpClass() {
driver = new RemoteWebDriver(new URL(hubAddress), capability);
}
#AfterClass
public static void setDownClass() {
driver.quit();
}
#Test
public void Test1(){
driver.get(...);
}
#Test
public void Test2(){
driver.get(...):
}
}
Or make it static in an TestSuite, with the same #BeforeClass/#AfterClass :
#RunWith(Suite.class)
#SuiteClasses({ Test1.class, Test2.class})
public class MyTestSuite {
public static WebDriver driver;
#BeforeClass
public static void setUpClass() {
driver = new RemoteWebDriver(new URL(hubAddress), capability);
}
#AfterClass
public static void setDownClass() {
driver.quit();
}
}
and
public class Test1 {
#Test
public void Test1(){
MyTestSuite.driver.get(...);
}
}

Related

Webdriver: close web driver only if test is successful

Webdriver: close web driver only if test is successful.
Is there a way to check test results in #After method?
public class FooTest {
private WebDriver webDriver;
#Test
public void testFoo() {
}
#After
public void cleanUp() {
if (isTestSuccess()) { // How?
webDriver.close();
}
}
}
If you are using Junit, you can pass the Scenario parameter to the tear down method:
#After
public void cleanUp(Scenario scenario) {
if (!scenario.isFailed()) {
webDriver.close();
}
}

How to programmatically run all test cases forever?

How to programmatically run all test cases forever ? Below is the setup I have … now I have to run this code infinitely …another requirement is I need to restart the app again after #aftersuite… I need to tear down the test cases because I need to produce report after each aftersuite …
For example :
public class SimpleTest extends TestBase
{
AppiumDriver driver;
#BeforeSuite
public void setUp() throws MalformedURLException {
// DesiredCapabilities and all setup
}
#Test(priority = 1)
public void testcase1()throws InterruptedException {
login();
}
#Test(priority = 2)
public void testcase2() throws InterruptedException {
//something
}
#Test(priority = 3)
public void testcase3() throws InterruptedException {
//something
}
#Test(priority = 4)
public void testcase4 throws InterruptedException {
//something
}
#Test(priority = 5)
public void testcase() throws InterruptedException{
//something
}
#Test(priority = 6)
public void testcase6() throws InterruptedException{
//something
}
#Test(priority = 7)
public void testcase_logout() throws InterruptedException {
logout();
}
#AfterSuite
public void testCaseTearDown()
{
driver.quit();
}
}
Create another class that would call these tests...
public SimpleTest {
public static void main(String[] args) {
while (true) {
org.junit.runner.JUnitCore.main("SimpleTest");
}
}
}
Arrange your tests in testNG.xml .
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Regression suite" verbose="1" >
<test name="SampleTests" >
<classes>
<class name="com.first.example.SampleTest"/>
</classes>
</test>
</suite>
Then programmatically run them with TestNG object.
import org.testng.TestNG;
import java.util.List;
public class RunTestNGXML {
public static void main(String[] args) {
TestNG testng = new TestNG();
List<String> suites = Lists.newArrayList();
suites.add("testng.xml");
testng.setTestSuites(suites);
String outputDir ="src"+File.separator+"reports";
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
while(true){
testng.setOutputDirectory(outputDir+File.separator+formater.format(calendar.getTime()));
testng.run();
}
}
}
The caveat with infinite loop is that your emailable-report would be overwritten in each iteration. So configure the test-output directory path with timestamps. Otherwise log4j appender can easily do the job of continuous logging.

driver object in the susbsequent test method is not getting executed

public class One {
public WebDriver driver;
#Test
public void test1() {
/*System.setProperty("webdriver.chrome.driver", "Y:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();*/
driver.findElement(By.id("username")).sendKeys("abc#ccp.com");
driver.findElement(By.id("password")).sendKeys("password!1");
System.out.println("im in first test case from demoTwo Class");
}
#BeforeMethod
public void test() {
System.setProperty("webdriver.chrome.driver", "Y:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://app.anywebsite.com");
System.out.println("im in first test case from demoONE Class");
}
#AfterMethod
public void afterMethod() {
// Close the driver
driver.quit();
}
}
How do I pass the driver object to subsequent test methods?
Sample code available for testng on web shows this kind of structure but none executes.
You just set your driver by this way:
#BeforeMethod
public void test() {
System.setProperty("webdriver.chrome.driver", "Y:\\chromedriver.exe");
this.driver = new ChromeDriver();
driver.get("https://app.anywebsite.com");
System.out.println("im in first test case from demoONE Class");
}

#BeforeClass does not start the tests

#BeforeClass does not start my tests in Webdriver, Java, and I don't know where do I go wrong
#BeforeClass
public static void setup() {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl + "login");
driver.findElement(By.id("username")).sendKeys("myUserName");
driver.findElement(By.id("password")).sendKeys("myPassword");
driver.findElement(By.id("loginBTN")).click();
}
After the code I start the regular testing:
#Test
public void firstTest() {
//myTestCode
}
After attempting to run, all tests fail, the webdriver does not start, etc...
It would be nice to have this since I have to test a page where I have to be logged in (with #Before the webdriver starts before each test, so obviously I would need the #BeforeClass for this one.)
#BeforeClass
public static void setup() {
//This needs to be here for this to run and having this here means its only local to this method
Webdriver driver;
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl + "login");
driver.findElement(By.id("username")).sendKeys("myUserName");
driver.findElement(By.id("password")).sendKeys("myPassword");
driver.findElement(By.id("loginBTN")).click();
}
Then your Test will work
#Test
public void firstTest() {
//myTestCode
}
Sample Code : Hopes this will work.
public class OpenBrowsers {
WebDriver driver = null;
#BeforeClass
public void beforeClass() {
System.out.println("beforeClass");
driver = new FirefoxDriver();
}
#Test
public void openGoogle() {
System.out.println("openGoogle");
driver.get("www.google.com");
}
#Test
public void openYahoo() {
System.out.println("openYahoo");
driver.get("www.yahoo.com");
}
#AfterClass
public void afterClass() {
driver.close();
System.out.println("afterClass");
}}

Remote PhantomJS driver in Junit

How do i configure selenium with a remote phantomjs drive in junit? I have been trying to find a tutorial for this but had no luck. My goal is to use this for testing in spring mvc for my single page application.
After some trial and error I have reached the following solution. This configuration is used in the Junit test class
private URI siteBase;
private static PhantomJSDriverService service;
private WebDriver driver;
protected static DesiredCapabilities dCaps;
#BeforeClass
public static void createAndStartService() throws IOException {
service = new PhantomJSDriverService.Builder().usingPhantomJSExecutable(new File("/path/to/phantom/driver"))
.usingAnyFreePort()
.build();
service.start();
}
#AfterClass
public static void stopService() throws IOException {
service.stop();
}
#Before
public void setUp() throws Exception {
siteBase = new URI("http://localhost:8080/");
dCaps = new DesiredCapabilities();
dCaps.setJavascriptEnabled(true);
dCaps.setCapability("takesScreenshot", false);
driver = new RemoteWebDriver(service.getUrl(),dCaps);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#After
public void tearDown() throws Exception {
driver.quit();
}
If you need further information comment below.

Categories