2

I'm Unable to get failed status if the script is stopped or element is not clickable getting all test pass how & what assert should I apply to get this resolved please help me package UsCheckoutUAT;

import static org.testng.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class EveMattressBaseUS{
  WebDriver driver;
  public static String result;
  StringBuffer verificationErrors = new StringBuffer();

  /**
   * @throws java.lang.Exception
   */
@BeforeClass(alwaysRun = true)
  public void setUp() throws Exception {
    System.setProperty("webdriver.chrome.driver","/usr/local/bin/chromedriver");
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
  }     
/**
 * @throws java.lang.Exception
 */
  //Adding product to the cart
  @Test(priority=0)
   public void AddToCart() throws InterruptedException  {
   try {
    driver.get("https://uat.sleepeve.com/");
    System.out.println("Title of page:  "+driver.getTitle());
    System.out.println("-----------------------------------------------------------------------------------");

    Actions action = new Actions(driver);
    WebElement Mattress =(WebElement)new WebDriverWait(driver,160).until(ExpectedConditions.elementToBeClickable
            (By.xpath("/html[1]/body[1]/header[1]/nav[1]/div[1]/a[2]/span[1]")));
    new Actions(driver).moveToElement(Mattress).click().perform();

    Thread.sleep(3000);

    WebElement submit2 =(WebElement)new WebDriverWait(driver,160).until(ExpectedConditions.elementToBeClickable
              (By.xpath("/html[1]/body[1]/div[8]/div[1]/div[2]/form[1]/div[2]/div[1]/button[1]")));
    new Actions(driver).moveToElement(submit2).click().perform();

    Thread.sleep(3000);

    }   catch (Exception E) {
         System.out.println("Exception in AddToCart: "+ E);
         }
  }


  @AfterClass(alwaysRun = true)
   public void tearDown() throws Exception {
      driver.close();
      driver.quit();
      String verificationErrorString = verificationErrors.toString();
      if (!"".equals(verificationErrorString)) {
        fail(verificationErrorString);
      }
    }

    private boolean isElementPresent(By by) {
      try {
        driver.findElement(by);
        return true;
      } catch (NoSuchElementException e) {
        return false;
      }
    }

    private boolean isAlertPresent() {
      try {
        driver.switchTo().alert();
        return true;
      } catch (NoAlertPresentException e) {
        return false;
      }
    }

    private String closeAlertAndGetItsText() {
      boolean acceptNextAlert = false;
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
          alert.accept();
        } else {
          alert.dismiss();
        }
        return alertText;
      } finally {
        acceptNextAlert = true;
      }

  }
}`
  • How and what are you currently asserting? – Alexey R. Sep 4 '18 at 7:42
  • Currently not asserting – Mahima soni Sep 4 '18 at 8:15
  • You have to give us some representative code. So that we are aware of what you have and what is missing. – Alexey R. Sep 4 '18 at 8:20
  • Currently not asserting recently tried some asserting but not getting the required solution, I want to do smoke testing in which I only want to navigate pages and clicks to button and links, i wrote simple script for this without using any assertion can you plz suggest me what assertion i have to use or any other required solution for the same – Mahima soni Sep 4 '18 at 8:21
  • Please refer code @AlexeyR. – Mahima soni Sep 4 '18 at 8:42
1

You're not able to get your tests failed because you catch the exceptions which are thrown within your tests.

enter image description here

This leads to the situation when all the exceptions which are thrown in your test code are caught and TestNG has just no idea about something wrong happened there.

You should:

  • either not wrap your test code in try-catch
  • or throw new exception in catch block.
  • or catch only exceptions which are irrelevant to your test result (Currently you catch all the possible exceptions which might occur)
| improve this answer | |
  • 1
    Thanks @Alexey it's working now thanx a lot for the quick reply – Mahima soni Sep 4 '18 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.