How can i get the steps from a specific failed scenario outline example in cucumber - cucumber-jvm

I want to trigger an email with the failed steps from a ,failing scenario outline feature file.
I need the java code which gives me the steps starting from Given , till the failing step definition.
Thanks in advance

You can create an After hook that gets control after a failure to gain access to the Scenario object.
#After
public void myAfterHook(Scenario scenario) {
if (scenario.isFailed()) {
System.out.println("***>> Scenario '" + scenario.getName() + "' failed at line(s) " + scenario.getLines() + " with status '" + scenario.getStatus() + "');
}
}
You can use the Scenario object to get
the name of the failing scenario, and
the line(s) in the feature file of the Scenario. Scenarios from Scenario Outlines return both the line of the example row the the line of the scenario outline.
You then use the line number(s) to refer back to the failing step in the feature file.

Related

QAF | How to fail a custom step?

I have created a custom step in which I'm doing some calculations. I need to pass or fail the step according to the outcome of the calculations. Presently step always shows pass in the report even when the calculation fails. Also, I would like to know how to pass the fail note to the report as I can see it is implemented in common steps.
I'm using QAF version 3.0.1
Below is a sample example:
#QAFTestStep(description = "I check quantity")
public static void iCheckQuantity() {
String product = getBundle().getString("prj.product");
Int availableStock = getBundle().getString("prj.aStock");
Int minStock = getBundle().getString("prj.minStock");
if (availableStock < minStock) {
// I want to fail the step here telling - minimum stock required to run the test for "product" is "minStock" but presenlty available is "availableStock"
}
}
I was able to figure out the answer.
import com.qmetry.qaf.automation.util.Validator.*;
assertFalse(true, "FAIL message here", "SUCCESS message here");
The below links were useful to understand more on verifications and also on different types of verifications/assertions available in QAF.
Refer:
https://qmetry.github.io/qaf/latest/assertion_verification.html
https://qmetry.github.io/qaf/latest/javadoc/com/qmetry/qaf/automation/util/Validator.html

How to fetch TestNG Eclipse Test run status info from Console to use it when required?

I am trying to use the TestNG Test run status information (i.e. the Passed Tests, Failed Tests, Skipped Tests).
Ex- Want to use this
Sanity_Suite
=======================
Total tests run: 6,Passed:4, Failures: 2, Skips: 0
=========================================
I have written a method that sends test status information via sms to a recipient. Here is the ->
I have used ITestReporter, ITestResult, TestNG and other related contexts to get the correct test run status, I am using TestNG and ExtentReporter both and trying to fetch the information through anyway round possible. But I am getting mixed or incorrect results.
(BELOW IS THE SMS RECEIVED)
Is there any suggestions to get it done?
I get that You are trying to send out sms with testresults.Could You please provide what is ITestResults object. Or to make it easier try with StringBuilder class, maybe it will ease up and fix Your issue:
StringBuilder sb = new StringBuilder();
sb.append("Titlle\n");
sb.append("Passed: " + cntPass + "\n");
sb.append("Failed: " + cntFail + "\n");
sb.append("Description: " + description +"\n");
return sb.toString();
From the given code, its actually really hard to see full picture.

Unpredictable behaviour with Selenium and jUnit

I am working on a website and trying to test it with Selenium and jUnit. I'm getting race conditions between the test and the site, despite my best efforts.
The front end of the site is HTML and jQuery. The back end (via AJAX) is PHP.
The site
I have two required text input fields (year and age), plus some others that I'm not changing in the tests that give problems. As soon as both text inputs are non-empty, an AJAX call is made to the back end. This will return 0+ results. If 0 results are returned, a results div on the screen gets some text saying that there were no results. If >0 results are returned, a table is written to the results div showing the results.
I don't want the site to wait until e.g. 4 digits' worth of year is entered before doing the AJAX call as it could be looking at ancient history (yes, really). So, as soon as both are non-empty the call should be made. If you type slowly, this means that entering e.g. 2015 will trigger calls for year=2, year=20, year=201 and year=2015. (This is OK.)
The test
I'm using page objects - one for the inputs and one for the output. At the start of the test, I wait for a prompt to be present on the screen (please enter some data) as that is generated by JavaScript that checks the state of the input fields - so I know that the page has loaded and JavaScript has run.
The wait for a prompt is made immediately after the page object is created for the output. This is the relevant method in the page object:
// Wait until the prompt / help text is displayed. Assumes that the prompt text always contains the word "Please"
public void waitForText() {
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("resultContainer"), "Please"));
}
The method for setting the year is
public void setYear(String year){
WebElement yearField = driver.findElement(By.id(yearInputId));
if (yearField == null) {
// This should never happen
Assert.fail("Can't find year input field using id " + yearInputId);
} else {
yearField.sendKeys(new String [] {year});
driver.findElement(By.id(ageInputId)).click(); // click somewhere else
}
}
and there's a corresponding one for age.
I have a series of methods that wait for things to happen, which don't seem to have prevented the problem (below). These do things like wait for the current result values to be different from a previous snapshot of them, wait for a certain number of results to be returned etc.
I create a driver for Chrome as follows:
import org.openqa.selenium.chrome.ChromeDriver;
// ...
case CHROME: {
System.setProperty("webdriver.chrome.driver", "C:\\path\\chromedriver.exe");
result = new ChromeDriver();
break;
}
The problem
Some of the time, things work OK. Some of the time, both inputs are filled in with sensible values by the test, but the "there are 0 results" message is displayed. Some of the time, the test hangs part-way through filling in the inputs. It seems to be fine when I'm testing with Firefox, but Chrome often fails.
The fact that there is unpredictable behaviour suggests that I'm not controlling all the things I need to (and / or my attempts to control things are wrong). I can't see that I'm doing anything particularly weird, so someone must have hit these kinds of issue before.
Is there a browser issue I'm not addressing?
Is there something I'm doing wrong in setting the values?
Is there something I'm doing wrong in my test choreography?
It could be that when you start typing, the script is still loading or that there's a pending Ajax call when you start handling the next field or validation.
You could try to synchronize the calls with a low level script :
const String JS_WAIT_NO_AJAX =
"var callback = arguments[0]; (function fn(){ " +
" if(window.$ && window.$.active == 0) " +
" return callback(); " +
" setTimeout(fn, 60); " +
"})();";
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
js.executeAsyncScript(JS_WAIT_NO_AJAX);
driver.findElement(By.Id("...")).sendKeys("...");
js.executeAsyncScript(JS_WAIT_NO_AJAX);
driver.findElement(By.Id("...")).click();

How to set a global Base URL for every test case of one test suite in Selenium IDE?

How to set a global Base URL for every test case of one test suite in Selenium IDE so that I can switch to different environment easily?
If you have a lot of test cases in a single suite, it's a pain to change the Base URL for each. Instead, create a separate case for each Base URL you need to switch between. For example, I have store https://testing.site.com/ as myEnvironment saved as test case SetEnvTesting. I create another case for my production site, SetEnvProduction.
Then, insert the ${myEnvironment} at the beginning of each relative URL in your test cases. For example, open ${myEnvironment}/login.aspx. (This might be a pain if you've got a lot of tests already. I suggest just adding it from now on.) Then, simply put the set-environment test case at the beginning of your test suite. To switch your entire suite to another Base URL, simply put a different set-environment case at the start. Another nice thing is that you can switch environments in the middle of the suite by putting a different set-environment case in the middle.
Edit: Example of using a SetEnvironment test case.
The SetEnvironment case:
An example of a following test case.
Notice
how the CurrentEnvironment variable is used. You can do this for every case in the suite. Additionally, you can make every separate test suite use this same SetEnvironment case, so they all switch together.
that the Base Url becomes irrelevant. You're overriding it, essentially.
I hope that's helpful!
The test case HTML file is responsible for setting the base URL. From the Selenium IDE, you can override the base URL for a test case. A test suite, on the other hand, is only a bag for your test cases.
Remove the base URL setting from each of the test case files referenced from your test suite. Then set the base URL by hand for the test suite and run the suite.
The line in question is in the head of each test case file:
<link href="http://server-name/" rel="selenium.base"/>
If it is missing, then the base URL overrides it automatically.
I found it exceedingly frustrating that I couldn't just have a blank Base URL and have it use whatever page I already had open as the start of the test. What is the point of being able to record relative URLs when it forces you to hardcode a base url that is prefixed to every relative url (thus turning them into absoulte urls)??
Extending on the other answers and from this question: In Selenium IDE, how to get the value of the base url
You can store the current domain in a variable and then use that instead of a hard coded one. This lets you login to the server you want to run the tests on, hit play and it will run the tests without all this baseURL nonsense redirecting you to the wrong server.
Just use these commands at the start of the first script in your suite:
And keep using the http://${host}/ syntax for every truly relative URL you need in your script.
I just created a separate test and put it at the top of all tests. Store the base url to an variable and now I am able to access it in other test cases by ${variable_name}.
I tried the accepted answer, it does work for selenium IDE, but it does not work when running stand-alone server.
finally, I put the value inside the user-extensions.js , and use storeEval to get the value back. Then the same value could be retrieved inside different cases.
EDIT:
In the user-extensions.js, add the following
var selenium_base_url = "http://example.com"
In the HTLM suite, add the following
<tr>
<td>storeEval</td>
<td>selenium_base_url</td>
<td>case_base_url</td>
</tr>
<tr>
<td>open</td>
<td>${case_base_url}/case_path?a=1&b=2</td>
<td></td>
</tr>
This may look odd at the first sight, but actually user-extension.js could be modified on the fly right before the stand-alone server execution, so you may put a different base url using a script in the real QA scenario.
Hmm, I think there is a simpler solution:
In the first test (of the test suite) adjust
<link rel="selenium.base" href="http://google.com" /> as needed
In the following tests remove (using text editor or similar):
<link rel="selenium.base" ... />
If you're fine double clicking multiple times,
Double click the top one, set it to the URL you want.
Now double click the top, middle, bottom.
Now double click the bottom, middle, top.
I created a user-extension command 'doBaseURL' that manipulates the Selenium IDE field 'Base URL'.
In every test case of a test suite I added this command | baseURL | | |. Below the surface it sets a stored variable named baseURL.
It is possible to give the stored variable 'baseURL' a value from a test case command prior to the first 'baseURL' command.
This method overwrites the <link href="http://server-name/" rel="selenium.base"/> setting of each test case file.
For me this works fine as now the Selenium IDE reflects the Base URL that the test case(s) will use (or was using when an error occured).
The java script is:
Selenium.prototype.doBaseURL = function(strTarget, strValue) {
/**
* Enables a more predictive Base URL when running a test suite with multiple test cases.
* On first usage it wil fill a stored variable 'baseURL' either with the given strTarget,
* or when not given with the Base URL as shown on the Selenium IDE UI. The Selenium IDE UI field
* Base URL is updated to reflect the (new) base URL.
* In each subsequent test case with a baseURL command the stored variable 'baseURL' will determine
* the base.
*
* <p> Best to use as first command in each test case and only in the first test case a strTarget
* argument, in that way the test cases of a test suite will no longer depend on their 'hidden'
* '<link rel="selenium.base" href="http://www.something.com/" />
* construction, that makes test cases effectively having an absolute base URL.
* </p>
*
* #param strTarget a Base URL value to be used in the test case (e.g. https://www.google.com/)
*/
// pushes the given strTarget into the selenium.browserbot.baseUrl
// and makes the new Base URL value visible on the Selenium IDE UI.
// A subsequent open command will use this new Base URL value
LOG.debug("begin: doBaseURL | " + strTarget + " | " + strValue + " |");
// note: window.location.href = strTarget; causes the browser to replace Selenium-IDE UI with the given url
// used that knowledge to manipulate the Selenium IDE UI as if a manual Base URL change was done.
LOG.debug("window.location.href= [" + window.location.href + "]");
LOG.debug("window.editor.app.getBaseURL() gives [" + window.editor.app.getBaseURL() + "]");
if (strTarget && (strTarget.substring(0,1)!="$")) { // only when strTaget has a value
strValue = "Base URL changed from [" + selenium.browserbot.baseUrl.toString() + "] into [" + strTarget + "]";
selenium.browserbot.baseUrl = strTarget.toString();
// set the visible Base URL value on the Selenium IDE UI to the new value
window.editor.app.setBaseURL(strTarget);
LOG.debug("updated into: " + window.editor.app.getBaseURL());
// remember this value for future calls
storedVars["baseURL"] = window.editor.app.getBaseURL();
LOG.debug("storedVars[\"baseURL\"] set to [" + window.editor.app.getBaseURL() + "]");
} else {
// no value => take storeVars["baseURL"] when set;
// otherwise take Selenium IDE Base URL field.
if ((storedVars["baseURL"] != undefined) && (storedVars["baseURL"] != "")) {
strValue = "Base URL changed from [" + selenium.browserbot.baseUrl.toString() + "] into [" + storedVars["baseURL"] + "]";
selenium.browserbot.baseUrl = storedVars["baseURL"];
// set the visible Base URL value on the Selenium IDE UI to the new value
window.editor.app.setBaseURL(storedVars["baseURL"]);
LOG.debug("updated from storedVars[\"baseURL\"]");
} else {
strValue = "Base URL changed from [" + selenium.browserbot.baseUrl.toString() + "] into [" + window.editor.app.getBaseURL() + "]";
selenium.browserbot.baseUrl = window.editor.app.getBaseURL();
// remember this value for future calls
storedVars["baseURL"] = window.editor.app.getBaseURL();
LOG.debug("storedVars[\"baseURL\"] set to [" + window.editor.app.getBaseURL() + "]");
}
}
LOG.info(strValue);
LOG.debug("end: doBaseURL | " + strTarget + " | " + strValue + " |");
}
Installing a user-extension script is explained in the "Using User-Extensions With Selenium-IDE" section SeleniumHQ documentation: http://www.seleniumhq.org/docs/08_user_extensions.jsp

jmeter - showing the values of variables

My group does a lot of test automation with JM. Typically we have a properties file which has a bunch of variables defined. These in turn are mapped to "User Defined Variables" of which we have a number of different sets.
These are in referenced throughout the rest of the jmx - I find it difficult as there are so many variables in so many different places to know what is what. Is there any way to have jmeter display what values its variables have - custom sampler is fine ? Ideally id love it if you could just hover a var and have its value displayed.
Any ideas ?
The newest versions of Jmeter have a fantastic sampler called "Debug Sampler" that will show you the values for: Jmeter Variables, Jmeter Properties or System properties.
You can insert them wherever you want in the script to get values at a given time. You'll want to have a "View Results Tree" enabled to view the sampler.
Given that Jmeter declares variables from a file on run, you won't be able to get your ideal solution.
I'm curious...would it be cleaner to employ "CSV Data Set Config" rather then populating "User Defined Variables" from a properties file?
Edit: Added explanation on variable declaration and asked CSV question.
Here is how I used to get Set of vars right through the code (variant with Java code in JSR223 PostProcessor):
Add "JSR223 PostProcessor" by right click wherever you need to check jMeter variables in your project;
Set Language (in my case - to java);
Add following code to Script window:
import java.util.Map;
String jMeterVars;
jMeterVars = "Quantity of variables: " + vars.entrySet().size() + ".\n\n";
jMeterVars += "[VARIABLE NAME] ==>> [VARIABLE VALUE]\n\n";
for (Map.Entry entry : vars.entrySet()) {
jMeterVars += entry.getKey() + " ==>> " + entry.getValue().toString() + "\n";
}
try {
FileWriter fw = new FileWriter("D:\\jMeterVarsForStackOverflow.txt",true);
fw.write(jMeterVars);
fw.close();
} catch(IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}
Check that everything in the JSR223 PostProcessor looks like that:
Start your project in jMeter.
The code above will create jMeterVarsForStackOverflow.txt file at D: and put all variables there: