How can I get the title of the current page?
Question ID: 417
1
0

Is there a way to get the title of the current page while executing a test?

Is there a way to get the URL of the current page while executing a test?

Marked as spam
Posted by (Questions: 4, Answers: 4)
Asked on June 28, 2023 9:03 pm
10 views

Answers (1)

0
Private answer

Alchemy is versatile in that, the user may easily extend its functionality.  You don't need to be an expert to do this either.  Anything similar to the aforementioned, can be accomplished by creating an Action in Alchemy.  For the official help see the section on Action Management.  For the do-it-yourselfer, that never reads the manual, just take a look at the existing actions in your install of Alchemy, under the Tools > Actions menu.  You can use the source code in them as examples, and build on that.  Using the installed actions as examples, we created 2 new Actions for our organization called "Get Page Title", and "Get URL".  Once we tested them, we promoted them so our whole "organization" could use them.  We came up with source code like this:

notes:

  • the line that gets the title is: webDriver.getTitle();   or the URL: webDriver.getCurrentUrl();
  • we included a line in each to print to the console "System.out.println", so we can monitor the progression of the execution from the console, you don't have to have that in there
  • Define an output variable to hold the result.  Write it to the variable using contextMap.put( contextName, title ); where "title" is the string where I placed the one that was retrieved from the page
  • When you add the Action to a test step, give the "Output Name" a value like "Current Page Title".

Get Page Title

String title;
try {
title = null;
title = webDriver.getTitle();
System.out.println( "Current Page Title: [" + title + "]");
} catch (Exception e) {
throw new IllegalArgumentException( "Unable to locate title", e );
}
if ( contextName != null ) {
contextMap.put( contextName, title );
}

Get URL

String u;
try {
u = null;
u = webDriver.getCurrentUrl();
System.out.println( "Current URL: [" + u + "]");
} catch (Exception e) {
throw new IllegalArgumentException( "Unable to get URL", e );
}
if ( contextName != null ) {
contextMap.put( contextName, u );
}

Marked as spam
Posted by (Questions: 0, Answers: 4)
Answered on July 26, 2023 1:48 pm
0
I forgot to mention, Once you promote your action, your Alchemy users can use this new action just as they would the pre-installed actions. They can be dragged from the "Actions Toolbox" into the test/function, or accessed while recording from the right-click context menu. Happy Testing!
( at July 26, 2023 1:52 pm)

Welcome to Alchemy Center!
Please Log In to participate in the discussion.

X
Scroll to Top