How do we use assert() in Selenium WebDriver?

0 votes

I want to use Assertion in selenium webdriver test beccause  on my screen, i have one 'edit' button, but that 'edit' button works only on certain conditions. So I just wanna check if that button is present, and if it is, then it should be clicked on and it should open another child window and perform certain actions. But, if that 'edit' button element is not present, then it should check the next condition which is log off button in my keyword framework. Below is my code, and I have used try and catch block and its working fine:

public void click_edit_cw2(String objectName)  {
        //Store the current window handle
        String winHandleBefore = driver.getWindowHandle();

        //Perform the click operation that opens new window
        try{
            WebElement element = driver.findElement(By.xpath("//*[@id='main']/div[1]/table/tbody/tr[2]/td[6]/button"));
        element.click();
        driver.findElement(By.xpath("//*[@id='main']/div[1]/table/tbody/tr[2]/td[6]/button")).click();

        //Switch to new window opened
        for(String winHandle : driver.getWindowHandles()){
            driver.switchTo().window(winHandle);
        }

        // Perform the actions on new window
        driver.findElement(By.xpath("//*[@id='myModal']/div/div/div[2]/form/div/div[10]/div/button[1]")).click();

        //Close the new window, if that window no more required
        //driver.close();

        //Switch back to original browser (first window)

        driver.switchTo().window(winHandleBefore);

        //continue with original browser (first window)
        }catch(Exception excptn){
            driver.findElement((By.xpath("//*[@id='logoutForm']/ul/li[2]/a"))).click();
    }
    }

But it didn't work for me with assert eventhough try() and catch() is not stopping the code. But after running the execution, it says test cases failed. How can assert that? I'm using keyword framework in which one class is for keyword and the other is for reading the excel file.

Apr 5, 2018 in Selenium by Martin
• 4,210 points
1,852 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

So, for implementing Assert(), you need to imlement test frameworks like TestNG or JUnit. I use TestNG and once the libraries  are imorted, I would run the below command:

Assert.assertTrue(driver.getWindowHandles().size().equals(2)); 

If the assertion fails, then my test case fails. This is one way of doing it, but since your using a keyword driven framework, you could modify the above line as per your wishe and get assertion working.

answered Apr 5, 2018 by nsv999
• 5,030 points

Related Questions In Selenium

0 votes
1 answer

How to use Actions class in Selenium Webdriver?

In seleniun webdriver it is not mandatory ...READ MORE

answered Apr 4, 2018 in Selenium by Damon Salvatore
• 5,250 points
1,814 views
+2 votes
1 answer

How can we handle authentication popup in Selenium WebDriver using Java

The Alert Method, authenticateUsing() lets us skip ...READ MORE

answered Apr 18, 2018 in Selenium by king_kenny
• 3,590 points
2,742 views
0 votes
1 answer

How do I get current URL in Selenium Webdriver using Python?

Use current_url element. Example: print browser.current_url READ MORE

answered Aug 8, 2018 in Selenium by Meci Matt
• 9,400 points
2,527 views
0 votes
1 answer

How to use webdriver in selenium?

In Java, in order to call the ...READ MORE

answered Dec 20, 2018 in Selenium by Shuvodip
54 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,030 points
51 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
413 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
40 views
0 votes
1 answer

How do we use Gecko driver.exe with Selenium?

Recently Selenium launched v3 and if you ...READ MORE

answered Apr 7, 2018 in Selenium by nsv999
• 5,030 points
37 views
0 votes
1 answer

How do i change the location where my file gets downloaded in Selenium Webdriver and firefox driver?

There are a couple of errors there. ...READ MORE

answered Apr 12, 2018 in Selenium by nsv999
• 5,030 points
788 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.