0

I have created a method which is as follows:

@Test(priority = 61)
public void Schedule_Valid_Promocode_Discount () throws InterruptedException
{
    WebElement Discount = driver.findElement(By.xpath("html/body/max-app-root/max-schedule/div/div[2]/max-campaign-information/div/aside/div[2]/div[3]/div/div[3]/div[2]/h4"));
    Assert.assertTrue(Discount!=null); // Check if discount appeared
    Assert.assertEquals("RM 1,000", Discount.getText());
}
public WebElement getSchedule_Discount_Amount () throws InterruptedException
{
    WebElement Discount = driver.findElement(By.xpath("html/body/max-app-root/max-schedule/div/div[2]/max-campaign-information/div/aside/div[2]/div[3]/div/div[3]/div[2]/h4"));
    return Discount;
}

I'm trying to store this value so that I can test the same value in upcoming test case. The test case is as follows:

@Test(priority = 79)
public void BillingDetail_InvoiceTab_DiscountAmount () throws InterruptedException
{
    //Check Discount amount on billing detail page
    WebElement InvoiceDiscount = driver.findElement(By.xpath("html/body/max-app-root/max-summary/div/div/div[2]/div/div/mat-card/mat-card-content/div[1]/div[1]/div/p[2]/span"));
    WebElement Discount = this.getSchedule_Discount_Amount();
    Assert.assertEquals(Discount.getText(), InvoiceDiscount.getText());
}

When I try to call this.getSchedule_Discount_Amount(); I get error - unable to locate element.

I have only one class and all my test cases falls under same class. I'm new to automation testing and not sure in what scenerios a new class should be created.

  • 1
    Can you please share your HTML code and instead of using absolute xpath try to use relative xpath for locating the elements. – shanila Jun 22 '18 at 7:11
  • When it runs @Test (priority = 61) it successfully finds the element. But when it comes to @Test (priority =72), it is still trying to find the element which I'm returning in test case 61. Surely that element is no more available during test case 72 because now we are on a different page. I want it to store the value in test 61 and validate value of test 72 with 61. – Sanchit Jun 22 '18 at 7:30
2

First, I strongly recommend you start working through any of the many basic Java tutorials that can be found on the web. You are correct that the discount element is no longer available because that element is on a different page than the invoice page you're using in your second test.

With some basic programming knowledge, you'd know that you'd need to declare a variable at the class level, then store the value of the element in test 61 so all your later tests can access it.

That is not the best way to handle this scenario. It creates a dependency between tests, which means that if test 61 fails, test 79 will also fail.

The better choice, if you have the ability to do this, is to either use a known good pre-existing invoice for test 79, or have test 79 do all the setup for its own invoice.

Psuedo code is below:

@Test(priority = 61)
public void Schedule_Valid_Promocode_Discount () throws InterruptedException
{
    NavigateToPage(pageUrl); //this is handled in a separate utility class
    string DiscountAmount = driver.findElement().getText(); // use id if the field has it, otherwise use the shortest path you can. Do not use absolute XPath because that will break every time the page layout changes.
    Assert.assertEquals("RM 1,000", DiscountAmount); // you only want one assert per test for clarity.
}

// other tests go here...

@Test(priority = 79)
public void BillingDetail_InvoiceTab_DiscountAmount () throws InterruptedException
{
    //Check Discount amount on billing detail page
    NavigateToPage(orderListPage);  // remember the separate class?
    OpenInvoice(knownInvoiceNumber); // this should open the invoice page
    string InvoiceDiscount = driver.findElement().getText(); //again, use id if the field has it.
    Assert.assertEquals(expectedAmountText, InvoiceDiscount);
}
0
  1. Avoid using assertTrue wherever it is possible. In your case it is better to use either Assert.assertNull or Assert.assertNotNull
  2. You won't be able to carry WebElement object through several tests. In your case you do not need WebElement since you anyway take text from it. So for your particular case I would suggest to:

    • Define class-level String variable (like discountReferenceText) where you'll be storing the text value of your Discount
    • Populate that variable in the method that is annotated with @BeforeClass. So once you test class starts before all the tests, you will initialize that value with driver.findElement(By.xpath("html/body/max-app-root/max-schedule/div/div[2]/max-campaign-information/div/aside/div[2]/div[3]/div/div[3]/div[2]/h4")).getText()

So that your test would eventually look like:

@Test(priority = 79)
public void BillingDetail_InvoiceTab_DiscountAmount () throws InterruptedException
{
    //Check Discount amount on billing detail page
    WebElement InvoiceDiscount = driver.findElement(By.xpath("html/body/max-app-root/max-summary/div/div/div[2]/div/div/mat-card/mat-card-content/div[1]/div[1]/div/p[2]/span"));
    Assert.assertEquals(discountReferenceText, InvoiceDiscount.getText());
}

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.