Testng Change Methode status from passed to fail - selenium

In my code i catch the error that might occure and give them a reporter.log(); which contain the problem that happened since i dont want my code to crash after one probleme found the probleme with this is that i always get Passed as a result even though my Report Log contains a failed step.
What i want is in case that reporter.log(); gonna be written i want to change the status of the methode into failed.
The report that i write :
String FormatMessageError = "<font color='red'></br><img src='"+ Bad +"'/>-";
Reporter.log(FormatMessageError +" Wasnt abel to connect</font>");
What i tried :
I used this :
Reporter.getCurrentTestResult().setStatus(ITestResult.FAILURE);
but the status stay as passed After that i tried this :
SoftAssert SAAjoutPanier = new SoftAssert();
SAAjoutPanier.assertEquals(ValidationOfPurchase,false,FormatMessageError +" Wasnt abel to connect</font>");
with : SAAjoutPanier.assertAll(); in the end of my #test as you can imagine it didn't work
Then i tried this :
Throwable throwable = new Throwable("One or more tests in this group has failed");
throwable.setStackTrace(new StackTraceElement[0]);
Reporter.getCurrentTestResult().setThrowable(throwable);
But it gives just a message but the status it self is still passed.
Can some one help me with this probleme ty !

MSTest has the same limitation. The solutuon I used, just throw a real error at the end.
throw new Exception("some test steps failed");
If that works, keep a list somewhere with the real errors and inserts in the error you throw.

Related

A resource failed to call close

I'm getting the android logcat message "A resource failed to call close". I've tracked it down to where that message gets generated. Here's the code:
Properties defaultProperties = new Properties();
URL propURL = Util.class.getClassLoader().getResource(DEFAULT_PROPERTIES_FILE);
if (propURL != null)
{
InputStream is = null;
try
{
// Load properties from URL.
is = propURL.openConnection().getInputStream();
defaultProperties.load(is);
is.close();
}
catch (Exception ex)
{
The message is generated on the call to "defaultProperties.load(is)".
I put a breakpoint on that line, and when I step over that line, the warning message is generated. I'm not the author of the code but that line gets executed at least two times and its the second time when that line gets called when the warning gets generated. I just don't see how under any circumstances that a resource failed to close would be generated on that line. I'm at a lost to explain how or why that error message would be generated there. Any ideas?
After thinking about this, I've come to the conclusion that the problem doesn't have anything to do with the line "defaultProperties.load(is)" causing the warning. Although the message is always generated the second time that line is called, my current thought is that the problem is happening elsewhere but when this line gets called it's probably yielding to some other VM related thread time to process, and that process is detecting that some resource failed to close. I'm concluding that the problem is related to something altogether different and calling that line is the time when the problem surfaces, but it's not what's causing the problem.

Console.ReadLine() throws nullpointer exception in Groovy

I started learning groovy yesterday and have been enjoying it thus far until I got to the part of the book where I have to take in inputs from the user using console().readLine() but I keep getting this error
Caught: java.lang.NullPointerException: Cannot invoke method readLine() on null object java.lang.NullPointerException: Cannot invoke method readLine() on null object
at com.usl.NewCodes.run(NewCodes.groovy:30)
I have tried it in intelliJ both as a groovy script and in a class. I even went on to try it in the groovyConsole yet it still throws an error.
Here is the code
print("What is your name ");
def fName = System.console().readLine()
println("Hello" + fName)
Thanks
If you are using any IDE for Groovy, the input / output console will not be available to you hence throwing null pointer exception so, you can use the following, If you are using Intellij enable groovy console from Tools -> groovy console for I/O.
println "What is your name?"
println "Your name is:"+System.in.newReader().readLine()

protractor ExpectedConditions.textToBePresentInElement returns false when the expected message has mora than one line

I'm facing a new problem with protractor ,
When I try to use this selenium method:
ExpectedConditions.textToBePresentInElement
It is returning false because my element returns this messsage:
Connection Unavailable.
Could not connect to the equipment at this time
This is my expected string:
message : Connection Unavailable.\nCould not connect to equipment at this time
then my method call is:
browser.wait(ExpectedConditions.textToBePresentInElement(element, message), 160000, 'The expected text is not present in the UI');
Maybe somebody can help me with it
Maybe it's not the best approach, but it'll solve your problem. Simply replace your expected messages with ones that have no \n:
browser.wait(ExpectedConditions.textToBePresentInElement(element, message.replaceAll("\\n", " ")),
160000, 'The expected text is not present in the UI');

F#: UnitTest case failled

Consider i have a function like newConvert. I am sopposed to recieve an error for newCovertor("IL") . To generate this erorr: I used failwith "Invalid Input"
The erorr is :
System.Exception: Invalid Input
> at FSI_0160.inputChecker(FSharpList`1 numberList) in C:\Users\Salman\Desktop\coursework6input\itt8060-master\coursework6input\BrokenRomanNumbers\Library1.fs:line 140
at FSI_0160.newCovertor(String romanNumber) in C:\Users\Salman\Desktop\coursework6input\itt8060-master\coursework6input\BrokenRomanNumbers\Library1.fs:line 147
at <StartupCode$FSI_0165>.$FSI_0165.main#() in C:\Users\Salman\Desktop\coursework6input\itt8060-master\coursework6input\BrokenRomanNumbers\newtest.fs:line 32
Stopped due to error
I used FsUnit and Nunit, they are loaded and installed and working rightly.
Then I made a tes for it using
[<TestFixture>]
type ``Given a Roman number15 ``()=
[<Test>]
member this.
``Whether the right convert for this number must be exist``()=
newCovertor("IL") |> should equal System.Exception
I cannot understand!! The function fails rightly, but the test does not accept it, so why??????
newCovertor does not produce a System.Execption - it throws it - so you never get to the should equal ... part
to catch the exception with FsUnit you have to wrap it as an action:
(fun () -> newCovertor("IL") |> ignore) |> should throw typeof<System.Exception>
also see the really good docs - there are quite a few ways to achieve this
The reason your version is not working is that NUnit will normaly mark a test as failed exactly when a exception is thrown inside the test-methods body - which you do.
If you wrap it in an action than the should throw can choose to execute this delayed action inside try ... match block to catch the expected exception and filter it out (or in this case throw an exception if there where none)
btw: just like in C#/VB.net you can also use the ExpectedExceptionAttribute from NUnit on the method level if you like - this way the test-framework will handle it for you.

Writing Codeception CLI test; want to throw exception but not have it fail the test

I forked Codeception/Codeception and added a Command module check that throws an exception.
When testing, this exception causes the test to fail but I'm running a test to force the exception and failure, to make sure it catches it.
Here's the edit to src/Codeception/Command/GenerateSuite.php
if ($this->containsInvalidCharacters ($suite)) {
throw new \Exception("Suite name '{$suite}' contains invalid characters. ([A-Za-z0-9_]).");
}
When updating tests/cli/GenerateSuiteCept.php when I run the command $I->executeCommand('generate:suite invalid-suite'); it fails because I'm throwing the exception in src/Codeception/Command/GenerateSuite.php in the first place.
Here is the addition to tests/cli/GenerateSuiteCept.php:
$I->executeCommand('generate:suite invalid-dash-suite');
When I run it, it says it failed. However, I want it to pass because I'm deliberately adding the dash to verify it catches invalid characters. What is the best way to do that? I'm wary of a try/catch block in these tests. I'm not sure if that is the best way to do that.
After looking at the other Codeception tests, I found this to be a better way than throwing an exception:
if ($this->containsInvalidCharacters ($suite)) {
$output->writeln("<error>Suite name '{$suite}' contains invalid characters. ([A-Za-z0-9_]).</error>");
return;
}
So I modified the test to:
$I->expect ('suite is not created due to dashes');
$I->executeCommand('generate:suite invalid-dash-suite');
$I->seeInShellOutput('contains invalid characters');