User Action Click on Defined or Undefined Link

Speedy993Speedy993 Posts: 21

I have a web app that I am creating a SOATest test for. In a table there is a link that may or may not be present. If it is there, I want to click on it. If it is not there, I want to have the test go to a different URL instead of the one that the test would have gone to if the link were present.
I have tried a few things such as hardcoding a URL but always get a casting error.. Here is what my Groovy script looks like now.
Any suggestions?
Thanks!

import com.parasoft.api.*;
import org.w3c.dom.*;
import webking.api.*;
import webking.api.browser2.*;

public Node clickSheriffLink(BrowserTestContext context)
{
// Get document
Document doc = context.getDocument();
// Get table element
Element table = doc.getElementById("chargeDetailForm:chargesTable");
if (table != null)
{
// Define index of column
int colIndex = 0;
// Count number of rows
String[] columnCells = WebBrowserTableUtil.getCellValuesForColumn(colIndex + 8, table);
int numRows = columnCells.length;
for (int i = 0; i < numRows; ++i)
{
if (!columnCells[i].contains("Address"))
{
// Go someplace else
// ???????? Element link = WebBrowserTableUtil.getLinkFromCell(colIndex + 8, row);
} else {
Element row = WebBrowserTableUtil.getRow(i + 1, table);
if (row != null)
{
// Return the link
Element link = WebBrowserTableUtil.getLinkFromCell(colIndex + 8, row);
//Application.showMessage("Row link: " + link);
if (link != null)
{
return link;
}
}
}
}
}
return null;
}

Comments

  • benken_parasoftbenken_parasoft Posts: 895 ✭✭✭

    but always get a casting error

    Can you share the exact error message and/or stack trace (double-click on error)? Often times the offending line number will be included in the error, showing you where your script is doing an illegal cast.

  • Speedy993Speedy993 Posts: 21

    Sorry, I posted the workaround version of my script. Basically, what I want to do is at the section where I have this:
    / Go someplace else
    // ???????? Element link = WebBrowserTableUtil.getLinkFromCell(colIndex + 8, row);
    I want to send the application to a new page. Because there is not a link in this cell I tried to just hardcode a URL. I also tried to copy the contents of another link with the HREF location changed. These are basically a long string and that is what results in the casting error. I am unsure what to replace the statement 'Element link = WebBrowserTableUtil.getLinkFromCell' with to get the test to go to a different webpage since there is not a link in that cell for this particular record.
    Does this rambling make sense?

  • tonytony Posts: 23
    edited May 5

    It looks as though you are trying to find an element using a script that will then click the element or, if the link is not available, navigate to a different page. Instead I believe you need to use Test Suite Logic to conditionally run a Click action or a Navigate action depending on an extracted value. You will want to:
    1. Add a test suite variable to the parent suite that will track if an anchor is present in the table cell or not. Let's call it "isAnchorAvailable"
    2. Add an output tool to the test where the table appears. This tool will check if an anchor is available and use context.setValue("isAnchorAvailable", theValue) to set our test suite variable
    3. Add a browser tool that clicks the table link, using a scripted locator to find the link in the table
    4. Add a browser tool that navigates to a different page
    5. Configure the test suite logic to only run the click browser tool if isAnchorAvailable is true
    6. Configure the test suite logic to only run the navigate browser tool if isAnchorAvailable is false

Sign In or Register to comment.