How to check a cookie name is “not” present on page using Katalon Studio? - cookies

I need to check that a cookie is "NOT" present on-page.
Based on this post, I have tried the following on the scripted Katalon mode:
added these to the standard import from the test case:
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import org.openqa.selenium.WebDriver as WebDriver
then I wrote:
WebUI.verifyMatch(driver.manage().getCookieNamed('foo'), is(null()))
and then I get the following error on null pointer
FAILED Reason:
java.lang.NullPointerException: Cannot invoke method call() on null object
Is there a way to write a check on "none" existing cookies using the script mode for Katalon Studio?
P.S:
I have tried this other approach
try {
_fbp = driver.manage().getCookieNamed('_fbp').getName()
}
catch (Exception e) {
String _fbp = new String('Something went wrong')
System.out.println('Something went wrong')
}
WebUI.verifyMatch('Something went wrong', _fbp, false)
It fails only on the verifyMatch part. It seems that 'something went wrong' does not really get stored in the variable _fbp.
FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: _fbp for class:

WebUI.verifyMatch() is used for checking matching between two strings.
You can do that using the plain Groovy assert. Instead of
WebUI.verifyMatch(driver.manage().getCookieNamed('foo'), is(null()))
do this:
assert driver.manage().getCookieNamed('foo').is(null)

Related

How to assert the content of a service message exception in a Grails unit test

I am using Grails 4.0.3 and want to assert that an exception is thrown in a given situation. Below, my PidService.
def validatePidIssuingConclusionDocument(JSONObject pidIssuingConclusionDocument){
String message = "Document issuing number is mandatory"
if(!pidIssuingConclusionDocument.has("docIssuingNumber")){throw new Exception(message)}
}
Below, my test case:
void "wrong document"() throws Exception {
JSONObject documentWithoutIssuingNumber = new JSONObject()
documentWithoutIssuingNumber.accumulate("docIssuingNumber","123")
pidService.buildPidIssuingOrder(documentWithoutIssuingNumber)
// How can I assert that the exception is thrown and verify it message.
}
I tried to use try/catch in the test case without success. Could anyone help me? I need to assert that the exception is thrown and the message is Document issuing number is mandatory in the given case.
I need to assert that the exception is thrown and the message is
Document issuing number is mandatory in the given case.
I can't provide an executable example around the code you provided because some things are missing (like buildPidIssuingOrder). See the project at https://github.com/jeffbrown/dnunesexception which contains an example that shows a way to deal with the exception in a test though.
grails-app/services/dnunesexception/PidService.groovy
package dnunesexception
import org.grails.web.json.JSONObject
class PidService {
def buildPidIssuingOrder(JSONObject pidIssuingConclusionDocument) {
throw new Exception('Document issuing number is mandatory')
}
}
src/test/groovy/dnunesexception/PidServiceSpec.groovy
package dnunesexception
import grails.testing.services.ServiceUnitTest
import spock.lang.Specification
import org.grails.web.json.JSONObject
class PidServiceSpec extends Specification implements ServiceUnitTest<PidService>{
void "test something"() {
when:
JSONObject documentWithoutIssuingNumber = new JSONObject()
documentWithoutIssuingNumber.accumulate("docIssuingNumber","123")
service.buildPidIssuingOrder(documentWithoutIssuingNumber)
then:
def e = thrown(Exception)
and:
e.message == 'Document issuing number is mandatory'
}
}

Qunit assert throws not working

I am trying to assert that a few steps in the code (visiting page, providing wrong card-number, password combination and clicking submit)should generate an error from the backend service - I have referred to this already..
and tried the suggestion of using Error Object as the second argument to assert.throws but that doesn't work for me.
i did see this link as well before posting my question,
My problem is - I don't have control over the code that throws the exception/error in this case. (I cannot change it to say Ember.assert etc) I just want to be able to catch an erroneous case.
Secondly, I don't have a component in this case. Its a straight forward API call that's made when click on submit is done, basically an action submitAuthForm is called in controller which calls ember cli mirage scenario that returns the following object problems object.
return new Response(401,{'X-Auth-Token': 'wrong-username-password-combination'},failResponse401);
and the returned object looks like
var failResponse401 = {
problems: [ {
field: null,
index: null,
value: null,
code: '0006',
subCode: '',
details: {},
_type: 'error'
} ]
};
We have a node_module dependency on an in-house exceptions kit that throws an Error object based on this.
Here's my Qunit test
test('ERROR_CASE_visiting /signon, provide cardNumber(2342314) and ' +
'password, submit and expect to see invalid cardnumber/password error',
function (assert) {
assert.expect(2);
assert.throws(
function () {
visit('/signon');
fillIn('#a8n-signon-card-number input', '2342314');
fillIn('#a8n-signon-password input', 'password');
click('#a8n-signon-submit-button button');
},
Error,
"Error Thrown"
);
});
I keep getting this error from Qunit
Error Thrown# 110 ms
Expected:
function Error( a ){
[code]
}
Result:
undefined
Diff:
function Error( a ){
[code]
}defined
Source:
at Object.<anonymous> (http://localhost:7357/assets/tests.js:175:12)
at runTest (http://localhost:7357/assets/test-support.js:3884:30)
at Test.run (http://localhost:7357/assets/test-support.js:3870:6)
at http://localhost:7357/assets/test-support.js:4076:12
at Object.advance (http://localhost:7357/assets/test-support.js:3529:26)
at begin (http://localhost:7357/assets/test-support.js:5341:20)
API rejected the request because of : []# 1634 ms
Expected:
true
Result:
false
Source:
Error: API rejected the request because of : []
at Class.init (http://localhost:7357/assets/vendor.js:172237:14)
at Class.superWrapper [as init] (http://localhost:7357/assets/vendor.js:55946:22)
at new Class (http://localhost:7357/assets/vendor.js:51657:19)
at Function._ClassMixinProps.create (http://localhost:7357/assets/vendor.js:51849:12)
at Function.createException (http://localhost:7357/assets/vendor.js:172664:16)
at Class.<anonymous> (http://localhost:7357/assets/vendor.js:133592:72)
at ComputedPropertyPrototype.get (http://localhost:7357/assets/vendor.js:32450:27)
at Object.get (http://localhost:7357/assets/vendor.js:37456:19)
at Class.get (http://localhost:7357/assets/vendor.js:50194:26)
at http://localhost:7357/assets/vendor.js:133645:30
Is there anything else that i can try to get it to work.
Is there someway i could somehow pass this test, by wrapping the returned response somehow in a way that it doesn't break my test altogether.
I found a workaround following this link
the user pablobm has posted a link to a helper
I used that to workaround this Qunit issue.

how test that the connection works in Doctrine 2?

I'm looking for a way to test if a connection is working or not with Doctrine 2.
As in my application users can change by themselves the information connections, I want to check if the user has entered the right login and password.
How can I do that?
I tried to put this code into a try/catch block :
try{
$entityManager = $this->getEntityManager() ;
$repository = $entityManager->getRepository('Authentification\Entity\User');
$userToIdentify = $repository->findOneBy(array('login' => $this->_username, 'password' => $this->_password));
}catch(Exception $e){
$code = Result::FAILURE ;
$identity = "unknow" ;
$messages = array(
"message" => "Wrong login/password combination",
) ;
}
The problem is that even if the information connection is correct, I cannot catch the exception.
Otherwise I get the following error :
<b>Fatal error</b>: Uncaught exception 'Zend\View\Exception\RuntimeException'
with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template
"layout/layout"; resolver could not resolve to a file' in C:\xampp\htdocs\poemsV3\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php:451 Stack trace: #0 C:\xampp\htdocs\poemsV3\vendor\zendframework\zendframework\library\Zend\View\View.php(203): Zend\View\Renderer\PhpRenderer->render(Object(Zend\View\Model\ViewModel)) #1 C:\xampp\htdocs\poemsV3\vendor\zendframework\zendframework\library\Zend\Mvc\View\Http\DefaultRenderingStrategy.php(128): Zend\View\View->render(Object(Zend\View\Model\ViewModel)) #2 [internal function]: Zend\Mvc\View\Http\DefaultRenderingStrategy->render(Object(Zend\Mvc\MvcEvent))#3 C:\xampp\htdocs\poemsV3\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(469): call_user_func(Array, Object(Zend\Mvc\MvcEvent))#4 C:\xampp\htdocs\poemsV3\vendor\zendframework\zendframework\library\Zend\EventManager\EventMa in <b>C:\xampp\htdocs\poemsV3\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php</b> on line <b>451</b><br />
Do you have any idea in how I could test if the connection works?
Thank you.
Do not use the EntityManager directly. You can instead use following to check the connection parameters:
try {
$entityManager->getConnection()->connect();
} catch (\Exception $e) {
// failed to connect
}
That's sadly the only real way to check if something went wrong, since the exception type changes depending on the driver you use.
For the other exception (the view-related one) you simply have to adjust your view scripts path. I suggest you to keep the skeleton application module enabled so that the default layout is always there: you can override it at any time.
You can use.
$cnx = $this->getDoctrine()->getConnection();
$cnx->isConnected() ?
'Connected' :
'not connected';

Selenium test to test cookie

I want to write a selenium test that will check that when I log in to my application, and then re-open the browser, I'm automatically logged in using the saved cookie.
I thought that this may be possible to calling clearSession() between two #{selenium} blocks, but that seems to clear the cookie too. I've tested that this functionality works manually.
Any ideas how I'd test this.
For reference: Here's what I've tried.
#{fixture delete:'all', load:'../conf/User.yml' /}
#{selenium}
deleteAllVisibleCookies()
// Open the home page, and check that no error occurred
open('/')
waitForPageToLoad(1000)
assertNotTitle('Application error')
open('/login')
type('usernameOrEmail', 'marchaos')
type('password', 'password')
clickAndWait('css=input[type=submit]')
assertTextPresent('Welcome marchaos')
clearSession()
#{/selenium}
#{selenium}
// Open the home page, and check that no error occurred
open('/')
waitForPageToLoad(1000)
assertTextPresent('Welcome marchaos')
#{/selenium}
it fails at the last assertTextPresent()
I don't know about Selenium but you can do this in a functional test.
I do something like this:
Response loginResponse = FunctionalTest.GET("/user/login?login=test&password=test");
Map<String, Cookie> loginResponseCookies = loginResponse.cookies;
....
Request request = FunctionalTest.newRequest();
request.cookies = loginResponseCookies;
request.url = url;
return FunctionalTest.GET(request, url);

ArrayOfAnyType issues when calling the method:GetRangeA1 excel web services in the silverlight 4.0

I create a simple silverlight 4.0 application used to read the excel file data in the share point 2010 server. I try to use the "Excel Web Services" but I get an error here when calling the GetRangeA1 method:
An unhandled exception of type 'System.ServiceModel.Dispatcher.NetDispatcherFaultException' occurred in mscorlib.dll
Additional information: The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://schemas.microsoft.com/office/excel/server/webservices:GetRangeA1Response. The InnerException message was 'Error in line 1 position 361. Element 'http://schemas.microsoft.com/office/excel/server/webservices:anyType' contains data from a type that maps to the name 'http://schemas.microsoft.com/office/excel/server/webservices:ArrayOfAnyType'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'ArrayOfAnyType' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.
the source code is like:
namespace SampleApplication
{
class Program
{
static void Main(string[] args)
{
ExcelServiceSoapClient xlservice = new ExcelServiceSoapClient();
xlservice.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
Status[] outStatus;
string targetWorkbookPath = "http://phc/Shared%20Documents/sample.xlsx";
try
{
// Call open workbook, and point to the trusted location of the workbook to open.
string sessionId = xlservice.OpenWorkbook(targetWorkbookPath, "en-US", "en-US", out outStatus);
Console.WriteLine("sessionID : {0}", sessionId);
//1. works fines.
object res = xlservice.GetCellA1(sessionId, "CER by Feature", "B1", true, out outStatus);
//2. exception
xlservice.GetRangeA1(sessionId, "CER by Feature", "H19:H21", true, out outStatus);
// Close workbook. This also closes session.
xlservice.CloseWorkbook(sessionId);
}
catch (SoapException e)
{
Console.WriteLine("SOAP Exception Message: {0}", e.Message);
}
}
}
}
I am totally new to the silverlight and sharepoint developping, I search around but didn't get any luck, just found another post here, any one could help me?
This appears to be an oustanding issue, but two workarounds I found so far:
1) Requiring a change in App.config.
http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/ab2a08d5-2e91-4dc1-bd80-6fc29b5f14eb
2) Indicating to rebuild service reference with svcutil instead of using Add Service Reference:
http://social.msdn.microsoft.com/Forums/en-GB/sharepointexcel/thread/2fd36e6b-5fa7-47a4-9d79-b11493d18107