Different action based on which mink driver is running - behat

I'm using Behat with Mink.
I would like one of my step definitions to act differently depending on which driver is running.
Ideally, my code would look something like this
public function stepDefinition(){
if($this->getSession()->getDriver()->name == 'goutte'){
//code to run if using goutte
}else{
//code to run if selenium is running
}
}

So, though a little bit of delving into the code meant that I've found a solution to this. And seen as google was no help, hopefully this will be of help to someone else.
My code now looks like this
if( $this->getSession()->getDriver() instanceof Behat\Mink\Driver\Selenium2Driver){
// Selenium Code
}else{
//Goutte Code
}
I've just grabbed the driver object and have checked which driver class it's an extension of, simple.
Now I can run the same step definition if the #javascript tag is or isn't before my scenario.

Related

Selenium:how to create test evidence report with NUnit

I was wondering how one would create proper test evidence of Selenium UI tests.
I was thinking about screenshots but they do not actually cover everything you have done as it is difficult to determine when to screenshot. (Every click or every wait or every pageload).
Another option I thought about was screen recording, but this makes it rather difficult to work with parallelism as you record the whole screen and not a specific chrome window.
However, you could possibly also take a screenshot every second via the webdriver and turn this into a video. Then one would have to work with a separate thread which can be quite challenging considering the condition you have to provide to stop the thread from taking screenshots. Else the test will run forever.
As I was not able to draw a convincing conclusion based on my own thoughts about creating a test evidence report for UI tests I was hoping someone could explain to me how to properly do this.
I had similar issue and I've introduced in my automation framework ExtentReports + klov server with Testrail as tool for test-management.
I think that nobody would ask of You to show testcases via video or screenshot, but if that is necessary, You can check out several libraries for taking 'video', because this is not actual video, than rather bunch of screenshots that are mashed into one video.
What has actually proven really good investment of time is to take fallen tests cases screenshot and attach it in testcase result (Testrail, Bugzila, Extentreports whatever).
Actualy if using selenium/appium You can check this repo
[https://github.com/groupon/Selenium-Grid-Extras] they make 'video' like mentioned and stored on local hub/node.
But best menthod that has been real good method was report with detail steps of each testcase:
Screenshot testcase with detailed steps and action:
The way I handled the reporting in my application goes in the line of what Kovacic said.
I also used ExtentReports as a way to generate metrics and having a step by step record of what happened.
I created a method reponsible for recording a step ( clicked that, navigated there, asserting that... ) with the option of taking a screenshot if needed, and another one for starting a new test.
Then , it's a matter of calling those methods in the PageObject style testing framework and pretty much having those method called in every action made by your framework.
To better illustrate here are some implementation examples (c#) :
Log a step method
public void LogStep(Status status,string MessageToLog, bool hasScreenshot)
{
//we leave the possibility of taking the screenshot with the step or not
if (hasScreenshot)
{
Test.Log(logstatus, messageToLog)
.AddScreenCaptureFromPath(GetScreenshot());
}
else
Test.Log(logstatus, messageToLog);
}
Screenshot capture method
public static string GetScreenshot()
{
ITakesScreenshot ts;
//Browser.Driver here is the instance of the Driver you want to take screenshots with
ts = (ITakesScreenshot)Browser.Driver;
var screenshot = ts.GetScreenshot();
// Here just input the name you want your screenshot to have, with path
var screenshotPath = ScreenShotFolder + #"\" + _screenshotcount + ".bmp";
screenshot.SaveAsFile(screenshotPath);
// I've introduced a variable to keep track of the screenshot count (optional)
return (ScreenShotFolder.Substring(_reportRoot.Length) +"/"+ _screenshotcount + ".bmp");
}
Example of call in the framework
public void BlockAccount()
{
try
{
_blockAccBtn.Click();
_confirmBtn.Click();
ExtentReportGenerator.LogStep(Status.Info, "Blocking Account");
}
catch (NoSuchElementException)
{
ExtentReportGenerator.LogStep(Status.Fail, "Could not find block button", true);
}
}
A NunitTest using the whole system
[TestCase, Order(1)]
public void CanBlockCard()
{
//Creates a new test in the report
ExtentReportGenerator.Test = ExtentReportGenerator.Extent.CreateTest(GetCurrentMethod());
//Each one of these calls to the framework has logged steps
CashlessPages.CashlessAffiliationsPage.AccessAccount(1, 1);
CashlessPages.CashlessAccountsPage.BlockAccount();
Assert.IsTrue(CashlessPages.CashlessAccountsPage.IsAccBlocked());
}
Example of generated Report
Hope this helps

Selenium: Check i the testcase pass or fail

Guys First of all I am totally new for Selenium.
I am having a automation project. In my project, I am creating a screenshot function to take screenshots of my event which I have created for my testcases. Now if my test cases passes then all screenshot should move to Pass folder, else fail folder.
I would like to know how to detect that my test case pass?
I know Nunit detects but I wanted to program it so that I cam place my screenshot as well as log file to pass or fail folder.
Program in C#
Selenium
Nunit to run my test case.
I think you meant was this. But there is work around for this. You need to add your code accordingly.
if (TestContext.CurrentContext.Result.Outcome.Equals(ResultState.Failure))
{
IntegrationTest.WriteInLog("FAILS");
}
else if (TestContext.CurrentContext.Result.Outcome.Equals(ResultState.Success))
{
IntegrationTest.WriteInLog("SUCESS");
}
Check status property and compare it with TestStatus enum at teardown method.
NUnit2:
TestContext.CurrentContext.Result.Status
NUnit3:
TestContext.CurrentContext.Result.Outcome.Status

If-else in testcomplete

I have application "Application" , which have same autorization service as skype, QQ etc (You must log using yours login/password)
I need to test some functionality in settings of this application, so I`m using testcomplete :
I need tu run application
Go to settings
Change something
Save
And its quite simple. But if you are logged of, I need to reproduce such scenario:
run application
1.1. if logged of - log using (testlogin/testpassword)
Go to settings
Change something
Save
How I can reproduce such functionality in TestComplete?
I`m newbie with it so I need help :)
Thanks
Make your test check whether the login window is displayed. You can do this using one of the Wait* methods. If the login window is displayed then call a test routine/keyword test that will perform a login and then continue the general test flow.
...
var loginWindow = Sys.Process("Application").WaitWinFormsObject("loginDialog", 3000);
if (loginWindow.Exists) {
doLogin();
}
...
function doLogin()
{
// perform login
}
While your answer is totally right, I'd like to know if this is possible:
If the loginWindow does not exist, TC gives an error, is it possible to ignore this error and keep it out of the log besides locking the log or disabling it?
Like in Java or C#:
if(object.Exists)
do something;
else
do other thing;
this throws an error in TC and I don't want it to because I'm already checking for the existence of the object...
You cannot use a method on a object that is not there.
Workaround would be to first check for an object and then to use the Exists method like so..
if (object && object.Exists) {
// doSomething
} else {
// doSomethingElse
}

Using if / else in selenium ide

I have a checkbox that I'm trying to click in Selenium IDE - but only if it's not already active.
I'm using Selenium IDE to create my tests, and htmlsuite to run them - anyone know how I can use an "if" in those?
You'll have to download the Flow Control plugin for Selenium IDE from the official page (aaaall the way down).
The most useful link I found is this one, because it has a complete example in it: http://selenium.10932.n7.nabble.com/if-else-statement-td4370.html
Anyway, there's also a documentation and author's blogpost explaining something more.
The only alternative I know about is implementing the whole logic in javascript - including the test steps. It's possible, it's a little bit harder to get right, but if you'll end up stuck with IDE without plugins, it might be your only save:
var value = this.browserbot.findElement("id=someInput").value;
if (value == "Slanec is the best!") {
this.browserbot.findElement("id=someButton").click();
}
Try this:
**storeTextPresent || [some_value] || [variable_name]**
**gotoIf || storedVars['variable_name']** == true || **goto_label_name**
// Command to execute if the condition is not met
**label goto_label_name**
// This is where the script will jump to when
// Command to execute if the condition is met, this part may be off course unrelated to the initial condition
You'll need to have installed the Flow Control plugin for Selenium IDE.

How to change the size of the browser window when running the FirefoxWebDriverProvider in JBehave Web

We're using JBehave Web to drive our selenium test suite for a new project and really like the Etsy.com example available on JBehave, especially the Java/Spring maven archetype as this fits in with our architecture.
The biggest problem so far has been documentation, which is why I'm posting here in the hopes that I can get some help from others in a similar situation.
It looks like JBehave Web only provides a "FirefoxWebDriverProvider" class and no corresponding one for Chrome. Has anyone else run into this problem? Have you written your own ChromeDriverProvider?
Also, we need to change the size of the browser that comes up by default and I can't seem to find a way of doing that during the bootstrapping of the test run.
We're using the Maven archetype: jbehave-web-selenium-java-spring-archetype which uses the jbehave-maven-plugin and the "run-stories-with-annotated-embedder" goal so we're using the "Annotated" method of extending the InjectableEmbedder.
If anyone can provide some guidance, I'd really appreciate it, even if just pointers to more examples.
How To Resize Window
webDriverProvider.get().manage().window().setSize(new Dimension(width, height));
You can easily find code like this by navigating through the code. If you are using Eclipse, Open Declaration and Quick Type Hierarchy options are everything you need.
How to Use Chrome Driver
You can use TypeWebDriverProvider or PropertyWebDriverProvider. For instance:
new TypeWebDriverProvider(ChromeDriver.class);
This should work:
driver.manage().window().setSize(new Dimension(800, 621));
What jokka said is correct, justr a side note: Before resizing window, I always put it to top left corner, so I know that WebDriver can "see" everything:
driver.get().manage().window().setPosition(new Point(0, 0));
Obviously, the driver above is assumed healthy instance of WebDriverProvider
We ended up finding this Chrome Driver and it's been working great. It can take a parameter when it is bootstrapped to start in maximized mode and also exposes the capability to add extensions when it starts up.
driver.Manage().Window.Size = new Size(x, y);
This works fine for me. The x and y are in a feature file.
Try this:
This is working fine for me.
Capybara.current_session.driver.browser.manage.window.resize_to(1800, 1000)