Sunday, February 15, 2015

Execute Stored procedure in SoapUI using Groovy Script

try
{
   sql.call("call stored_proc.name('111', '222', 'xxx');")
}catch (Exception ex)
{
   log.info ex.message
}catch (Error er)
{
   log.info er.message
}

Run test step depending on status of a different test step in SoapUI using Groovy Script

testRunner.testCase.testSuite.project.name
import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus
import com.eviware.soapui.impl.wsdl.WsdlTestSuite
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep


myTestStepResult = testRunner.testCase.testSuite.getTestCaseByName("testCase_1").getTestStepByName("testStep_2").run(testRunner, context)
myStatus = myTestStepResult.getStatus()
if (myStatus == "OK")
testRunner.gotoStepByName("testStep_3")
else{
def tCase = testRunner.testCase.testSuite.testCases["Name of test cases"]
def tStep = tCase .testSteps["test step you want to run"]
tStep.run(testRunner, context)
}

List Names of Test cases in Test Suite in SoapUI using Groovy Script

import com.eviware.soapui.impl.wsdl.teststeps.*
def i=0;
for( testCase in testRunner.testCase.testSuite.getTestCaseList() ) {
    for( testStep in testCase.getTestStepList() ) {
        if( testStep instanceof WsdlTestRequestStep ) {
            log.info i++ +"Name of testcases in this suite: " +"[" +testCase.getLabel() +"]" +" Name of the WSDL step is " +testStep.getName();
        }
    }
}

Wednesday, February 4, 2015

How to handle alert and confirm boxes and avoid the Race Condition

Method 1
-----------
Alert alert = null;
Wait<WebDriver> wait = new WebDriverWait(driver, 10, 50);
try {
   alert = wait.until(ExpectedConditions.alertIsPresent());
} catch (TimeoutException ignored) {
}
if (alert != null) {
   alert.accept(); // this line throws the exception
}

Method 2
-------------

public void clickOkOnAlertDialog(String dialogText) {

        Sync sync = Sync.start(20);
        while (sync.continueWaiting()) {
            wait = new WebDriverWait(driver, pageTimeoutInSeconds);
            wait.until(ExpectedConditions.alertIsPresent());
            Alert statusConfirm = driver.switchTo().alert();
            junit.framework.Assert.assertEquals(dialogText, statusConfirm.getText());
            try {
                statusConfirm.accept();
                return;
            } catch (WebDriverException e) {
                sync.sleep(250);
            }
        }
        throw new TimeoutException("Unable to locate and dismiss alert dialog: " + dialogText);
    }
   
Method 3 (Not a good way though)
-----------

((JavascriptExecutor) driver).executeScript("window.alert = function(msg){};");
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg){return true;};");

How FirefoxDriver opens Firefox Browser 
A beta features which makes Firefox Faster 
Enable Firebug in Session started by FirefoxDriver 
How FirefoxDriver Executes the Selenium Commands 

How FirefoxDriver opens the firefox browser?

When the following line is executed

FirefoxDriver driver = new FirefoxDriver();

1)Grab the port
    a)By default firefox attempts to grab the port 7055
    b)7055-1 = 7054, This is called as locking port, which we use as a mutex to make sure only one instance of firefox is started at the time
    c)Sit in a loop waiting the the locking port to become free
    d)Exit loop when we can establish a connection to the server socket(A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester.) to the locking port. This driver now has the mutex

2)Find the next free port up from the locking port
    Identify the next free port above the locked one by attempting to bind to each of them in turn. Release the newly found port. For the first driver, this will be 7055.

Until this point the FirefoxDriver has created a socket and FirefoxDriver is listening at the port 7055 for the incoming commands that needs to be executed
But we have not yet launched the browser

3)Locate the Firefox profile to use. This is normally an anonymous one

4)Copy the existing profile to a temporary directory,

5)Delete the current "extensions.cache" to force Firefox to look for newly installed extensions on start up.

6)Update "user.js" with the preferences required to make WebDriver work as expected.
(This is where the Capabilities are set for the browser, Eg EnableJavascript etc)

7)Set the "webdriver_firefox_port" value to the one that we found in the step 2.

Until this point the FirefoxDriver has created a socket and FirefoxDriver is listening at the port 7055 for the incoming commands that needs to be executed
We have selected the profile(default) or the one given from code and the Desired Capabilities are set

8)Start Firefox with the "-silent" flag and telling it to use the freshly minted directory as the profile.
Finally, we launch the Firefox Browser which we can see..!!

9)Wait until that Firefox instance has finished and the process has died.

10)Release the locking port. The mutex has now been freed.

Handle Race Condition in Alerts with Webdriver 
A beta features which makes Firefox Faster 
Enable Firebug in Session started by FirefoxDriver  
How FirefoxDriver Executes the Selenium Commands 

A Beta feature which makes Firefox Webdriver considerable fast


There is beta feature to make firefox not wait for the full page to load after calling .get or .click. This may cause immediate find's to break, so please be sure to use an implicit or explicit wait too. This is only available for Firefox and not other browsers.

   FirefoxProfile fp = new FirefoxProfile();
   fp.setPreference("webdriver.load.strategy", "unstable");
   WebDriver driver = new FirefoxDriver(fp);

Handle Race Condition in Alerts with Webdriver 
How FirefoxDriver opens Firefox Browser 
Enable Firebug in Session started by FirefoxDriver
How FirefoxDriver Executes the Selenium Commands

How to Enable Firebug in the Mozilla session started by Webdriver

Download the firebug xpi file from mozilla and start the profile as follows:

   File file = new File("firebug-1.8.1.xpi");
   FirefoxProfile firefoxProfile = new FirefoxProfile();
   firefoxProfile.addExtension(file);
   firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); // Avoid startup screen

   WebDriver driver = new FirefoxDriver(firefoxProfile);

Handle Race Condition in Alerts with Webdriver 
How FirefoxDriver opens Firefox Browser 
A beta features which makes Firefox Faster 
How FirefoxDriver Executes the Selenium Commands 

How Firefox executes the Selenium commands

//How webdriver sends commands to Firefox
--------------------------------
1)Webdriver creates a map/dictionary(based on programming language) with

context : String, opaque key by client language(Redundant)
commandName : The name of the command to execute
parameters : A list of parameters, The typoe depends on the command, This can be empty
elementId : String and an opaquekey that represents the identifier for a particular element

2)Serialize the map using JSON

3)Connect to the socket opened by the Firefox

4)Send the JSON across wire to Firefox

5)It will be Block until the response is sent

//Firefox executes the command
------------------------------

6)Once the command has reached the Firefox, it is deserialized from JSON to Javascript Object

7)The command name is interpreted

8)Some commands don't pay attention to context, for some that do, context is interpreted as window or frame.

9)In order to execute the command, it is looked up in FirefoxDriver prototype.

     eg :
     String url = "http://google.com"
     driver.get(url);

is same as

FirefoxDriver.prototype.get = function(respond, url)

Each function takes at least one parameter: the object that represents the response

FirefoxDriver.prototype.getCurrentUrl = function(respond) {
respond.context = this.context;
respond.response = Utils.getBrowser(this.context).contentWindow.location;
respond.send();
}

10) Calling the "send" method causes the response to be sent via the socket connection back to the client

//Webdiver reads the response
------------------------------

11)The response is another map serialzed in JSON,

methodName : String, Should match "commandName" from the original command sent
context : same as what was sent
isError : Indicated some error has occured(In java this will cause the exception)
responseText A string who's meaning differs depending on the command.

12)If "isError" is true, this will be a description of the error that actually occurred.

Handle Race Condition in Alerts with Webdriver 
A beta features wihich makes Firefox Faster
How FirefoxDriver opens Firefox Browser
Enable Firebug in Session started by FirefoxDriver 
How FirefoxDriver Executes the Selenium Commands