Webdriver: close web driver only if test is successful - java

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();
}
}

Related

TestNG groups doesn't execute #BeforeMethod

I follow tutorial to execute TestNG groups in Eclipse
The problem it doesn't execute #BeforeMethod method which setup data so all tests in group failed
Method is executed when running test(s) regular,meaning each test or all tests in class
Is it limitation of TestNG or I'm missing something?
#BeforeMethod
public void setup() { } //...
#Test(groups = { "mygroup" })
public void testMyGroup1() { }//...
#Test(groups = { "mygroup" })
public void testMyGroup2() { }//...
public void testNotInGroup() { }//...
You will need to add "alwaysRun=true" in configuration methods if they need to be executed irrespective of group.
#BeforeMethod(alwaysRun = true)
public void setup() { } //...

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");
}

Test design for taking screenshot with Selenium WebDriver on failed JUnit tests

I'm running into a strange JUnit and Selenium issue.
I want to write a screenshot taker that will take a screenshot on failed tests.
I define the screenshot taker class as:
public class SeleniumJUnitScreenshotTaker implements MethodRule
{
private WebDriver driverLocal;
public SeleniumJUnitScreenshotTaker(WebDriver driver)
{
driverLocal = driver;
}
public void takeScreenshot(String fileName)
{
try
{
new File("test-results/scrshots/").mkdirs(); // Make sure directory is there
FileOutputStream out = new FileOutputStream("test-results/scrshots/" + fileName + ".png");
out.write(((TakesScreenshot) driverLocal).getScreenshotAs(OutputType.BYTES));
out.close();
}
catch (Exception e)
{
// No need to crash the tests here
}
}
public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o)
{
return new Statement()
{
#Override
public void evaluate() throws Throwable
{
try
{
statement.evaluate();
}
catch (Throwable t)
{
takeScreenshot(frameworkMethod.getName());
throw t; // rethrow
}
}
};
}
I've a base test class where I setup the WebDriver as follows:
public class TestBase
{
protected static WebDriver driver;
#Rule
public SeleniumJUnitScreenshotTaker seleniumJUnitScreenshotTaker = new SeleniumJUnitScreenshotTaker(driver);
.
.
.
#BeforeClass
public static void intialize()
{
.
.
driver = SeleniumWebDriverFactory.getWebDriver(browser.toString());
if ((browser != Browser.Chrome) && (browser != Browser.ChromeCanary))
{
driver.manage().window().maximize();
}
hostUrl = Configuration.getHostUrl(envUnderTest);
// Open Browser and navigate to main page
driver.navigate().to(hostUrl);
}
#Before
protected void setup() throws Exception
{
this.users = getUsers();
}
#After
protected void teardown()
{
this.testCases.cleanupChecks();
}
#AfterClass
public static void terminate()
{
// Shut down WebDriver
if (driver != null)
driver.quit();
SeleniumWebDriverFactory.stopDriverService();
}
}
The problem is when I try to run a series of tests in the test class (derived from TestBase), the tests after the first failure throws TimeoutException. This is because we don't close the current browser between tests and the new test tries to find things that are absent in current browser.
Is there a way to close browser in between tests but not close the driver? Or reinstantiate the driver between tests? Else is there an alt. design that can make this work?
Thanks
Yana

#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");
}}

Opening Selenium Webdriver tests in the same window

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(...);
}
}

Categories