Is it possible to ignore Firefox for running the feature? - selenium

I am trying to use tags for ignoring Firefox for running few feature files.
I mean something like:
#Browser_Chrome
Feature: My Functionality
#ignore #Browser_Firefox
Scenario:
Given blablabla"
Is it possible?
Now looks like #ignore tag is working in any way

You can always check within your tests using getCapabilities(https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/remote/RemoteWebDriver.html#getCapabilities--):
assumeTrue( !(driver as RemoteWebDriver).capabilities
.getCapability("browserName").equals("firefox") ) )
More info about assume: https://junit.org/junit4/javadoc/4.12/org/junit/Assume.html

If you want to run almost all tests on Firefox, but want to exclude some. I would use a tag with a matching beforescenario which does the ignoring.
Feature file:
#BeforeExcludeFF #ThisIsMyFeature
Scenario: Dont run this when the browser is Firefox
Given blablabla
When blablabla
Then blablabla
Code behind:
[BeforeScenario("BeforeExcludeFF", Order = 1)]
public void BeforeExcludeFF()
{
// we need to get the current browser to know if it is firefox
ICapabilities capabilities = ((RemoteWebDriver)driver).Capabilities;
string browser = capabilities.GetCapability("browserName");
if(browser.ToLower() == "firefox")
{
// don't forget to log WHY we ignore this scenario on FF
LogToConsoleAndIgnore(#"We dont want to run this scenario because ");
}
}
private void LogToConsoleAndIgnore(string reason)
{
Console.WriteLine(reason);
Assert.Ignore(reason);
}
Hope this helps.

Related

Reading parameters from TestNG file

I successfully implemented several tests within TestNG framework, where parameters are being read from xml file.
Here is the example block that is executed as first:
#Parameters({ "country" })
#BeforeSuite(alwaysRun = true)
public void prepareRequest(String country, ITestContext cnt) {
LoginInfoRequestParm loginParms = new LoginInfoRequestParm(country);
Headers reqHeaders = new Headers();
reqHeaders.setHeaders(loginParms);
}
The problem/question is, why does it work only if the ITestContext is specified? Once it is removed, the overall suite is broken and it will never come to the specified method prepareRequest(). I was not able to debug it, because I cannt set breakpoint before the method to be able to see what is going on in TestNG itself.
Thank you for your explanation.
To get out of this situation, try something like this
String myPar = context.getCurrentXmlTest().getParameter("country");
if (myPar == null) {
myPar = "INDIA";
}
now myPar can be used, only thing here is if you run class for debug or any other purpose then we are using INDIA. if we run from testng.xml file then it will take values from that file.

Spring shell 2.0 how to read inputs with mask

Is there any way to mask user inputs in Spring Shell 2.0.x ?
I need to collect password from user.. did not find any shell api to do that.
Thanks!!
Found that LineReader#readLine(msg,mask) provides that option.
All you have to do is inject the LineReader bean.
If you don't want to rely on third party libraries, you can always do a standard Java console input, something like:
private String inputPassword() {
Console console = System.console();
return new String(console.readPassword());
}
Note though that when running this in an IDE, the System.console() might be null. So you should probably handle that if running in an IDE is something you want to support (for testing for example..) something like:
private String inputPassword() {
Console console = System.console();
// Console can be null when running in an IDE
if (console == null) {
System.out.println("WARNING - CAN'T HIDE PASSWORD");
return new Scanner(System.in).next();
}
return new String(console.readPassword());
}

how effectively i can use ui automation recorded test cases for other releases of the application

I've web application, I want recorded test cases and play back that cases.
1st release of the application, I've login module which has user name and password and recorded 500 test cases for entire application. Among 500 test cases 200 test cases are using logging by username and password.
2nd release of the application, I've login module which has only username, so I want use previous recorded test cases by modifications not like go to all the test cases change the password field. Here I'm having some requirements for the testing framework
Can I get what are test cases will effect by changing field like in above example?
Is there any way to update in simple, not going like in all the files and changing
I've used different UI Automation testing tools and record & Play back options are very nice, but I could not find the way I want in the UI Automation test framework.
Is there any Framework available which does the job for me?
Thanks in advance.
This is a prime example of why you never should record a selenium test case. Whenever you want to update something like login you have to change them all.
What you should do is create a test harness/framework for your application.
1.Start with creating a class for each webpage with 1 function for each element you want to be able to reach.
public By username(){
return By.cssSelector("input[id$='username']"); }
2.Create helper classes where you create sequences which you use often.
public void login(String username, String password){
items.username().sendkeys(username);
items.password().sendkeys(password);
}
3.In your common test setup add your login function
#BeforeMethod(alwaysRun = true)
public void setUp() {
helper.login("user","password");
}
This give you the opportunity to programmaticly create your test cases. So for example if you want to use the same test cases for a different login module where password element is not present it could be changed like this.
items.username().sendkeys(username);
if(isElementPresent(items.password())
items.password().sendkeys(password);
The function "isElementPresent" could look like this
public boolean isElementPresent(By locator){
try {
driver.findElement(locator);
logger.trace( "Element " + stripBy(locator) + " found");
} catch (NoSuchElementException e) {
logger.trace( "Element " + stripBy(locator) + " not found");
return false;
}
return true;
}

Unable to find browser object in RFT 8.5

I installed RFT 8.5 and JRE 7. When I run the scripts it's not finding browser object.
Below is the code which I have used in RFt to find the brwoser object.
Dim Allobjects() as TestObeject
Allobjects=RootTestObject.GetRootTestObject.Find(".class","Html.HtmlBrowser"))
Here it's is returning Allbects.lenth=0. Because of the I am getting struck.
Anybody can help me how to resolve this issue.
Note: I am using IE8
I was not able to find the browsers using RootTestObject either. But it is possible to find the browser windows using the Html domains:
startApp("Google");
startApp("asdf");
sleep(5);
DomainTestObject[] dtos = getDomains();
List<DomainTestObject> htmlDomains = new ArrayList<DomainTestObject>();
for (DomainTestObject dto : dtos) {
if (dto.getName().equals("Html")) {
htmlDomains.add(dto);
}
}
List<BrowserTestObject> browsers = new ArrayList<BrowserTestObject>();
for (DomainTestObject htmlDomain : htmlDomains) {
TestObject[] tos = htmlDomain.getTopObjects();
for (TestObject to : tos) {
if (to.getProperty(".class").equals("Html.HtmlBrowser")) {
browsers.add((BrowserTestObject) to);
}
}
}
System.out.println("Found " + browsers.size() + " browsers:");
for (BrowserTestObject browser : browsers) {
System.out.println(browser.getProperty(".documentName"));
}
Output:
Found 2 browsers:
https://www.google.ch/
http://www.asdf.com/
First, I start 2 browsers. Then I get all Html domain test objects. After that, I get all top objects and check whether their class is Html.HtmlBrowser.
I hope there is a simpler solution—looking forward to seeing one :)
Try the below code Snippet:
Dim Allobjects() As TestObject
Allobjects = Find(AtDescendant(".class", "Html.HtmlBrowser"))
Hope it helps.
Browser is a toplevel window so what you can do is :
Dim Allobjects() as TestObeject
Allobjects=Find(AtChild(".class","Html.HtmlBrowser"))
'The above code expects the browser to be statically enabled , also RootTestObject is not needed as implicitly RFT will use the RootTestObject if no anchor is provided.
Also if the browser is not statically enabled then you could also use:
DynamicEnabler.HookBrowsers() API so that browsers get enabled.

Is it possible to skip tests if an element doesn't exists?

I'm writing a test script for a website. The website has tabs (navigation link).
Let's say, the element of that tab is id=email.
If that doesn't exist, is it possible to skip the whole test. All test cases are based on that tab (id=email).
Right now, I have:
if($this->isElementPresent("id=email") == true) {
perform these steps
}
And all the test scripts are like that, so it's just opening the browser and closing it without testing anything. It's passing them all. Is it possible to skip tests if that element doesn't exists?
I would configure the test to use the same setting to see if the fields exist or not, instead of skipping tests. Mock your configuration, and set to disabled, then the tests should look for the absence of the fields, and test accordingly. Then, set the configuration to be enabled, and test that the field is there and test accordingly.
When the field is set to be disabled, you can also use the $this->markTestSkipped(). It is documented in the PHPUnit help Chapter 9. Incomplete and Skipped Tests.
Sample:
public function testEmailIdAbsent()
{
if($this->MockConfiguration['Email'] == 'disabled') // Or however your configuration looks
{
$this->assertFalse($Foo->IsElementPresent("id=email", "Email ID is present when disabled.");
...
}
}
public function testEmailIdPresent()
{
if($this->MockConfiguration['Email'] == 'enabled') // Or however your configuration looks
{
$this->assertTrue($Foo->IsElementPresent("id=email", "Email ID is not present when enabled.");
...
}
}
public function testEmailId()
{
if($this->MockConfiguration['Email'] == 'disabled') // Or however your configuration looks
{
$this->markTestSkipped('Email configuration is disabled.');
}
}