I'm currently finding the current url with
expect(browser.getCurrentUrl()).toEqual("example.com");
I want to make it into an if statement that would the url as the condition. How could I do that?
if(browser.driver.getCurrentUrl().toEqual("example.com") == "example.com"){
//do stuff
}
This is what I tried and it did not work
getCurrentUrl() is a promise, so you need to resolve it yourself to use the value as a conditional.
browser.getCurrentUrl().then((url) => {
if(~url.indexOf('example.com')) {
// code
}
else {
// code
}
})
Finally, Jasmine methods like toEqual should be used strictly with test assertions. I wouldn't use them outside of an expect statement like you have above.
Related
I'm trying to generate random int and check if the integer is exists in Firebase database, and do this infinitly until the integer is not exist inside database.
Below my code:
while (true) {
_changeRandomInt()
firebaseFirestore.collection("myCollection").doc(_randomInt).get().then((DocumentSnapshot documentSnapshot) {
if (getFromFirebase(documentSnapshot) == null) {
break;
}
});
}
But I cannot even run this code. Error: A break statement can't be used outside of a loop or switch statement. Try removing the break statement. I have my break inside while loop, then why I'm getting this error? How to fix this?
A break statement can't be used outside of a loop or switch statement. Try removing the break statement.
This is self explanatory. You can't use break outside of a loop. Which in your case is inside a future.
Do something like this:
_changeRandomInt();
firebaseFirestore.collection("myCollection").doc(_randomInt).get()
.then((DocumentSnapshot documentSnapshot) {
while (getFromFirebase(documentSnapshot) != null){
//the body of this loop will only execute as long as the value isn't null
//will break out of the loop as soon as the value is null
}
}
The break you have used is inside then(), which means no loop is available immediately above `break. You can refactor your code in this manner to get it working.
while (true) {
_changeRandomInt()
final documentSnapshot = await firebaseFirestore.collection("myCollection").doc(_randomInt).get();
if (getFromFirebase(documentSnapshot) == null) {
break;
}
}
If getFromFirebase is also a future, then await that as well inside if block.
You want to do something like this:
void functionName() async {
try{
var ans;
while(getFromFirebae(ans)== null){
ans = await firebaseFirestore.collection("myCollection").doc(_randomInt).get();
}
} catch(e){
//error
}
}
However, this is not the correct way. A good practice is to create a loading widget, that would show an animation while the future is loading. An even better way - to load everything in the background without stalling the overall app.
Some people have been saying that you're using "break inside a Future", but that's not quite right. Your problem is that you're using break inside of a separate function body. This is equivalent to writing:
void f() {
break;
}
which doesn't make sense since break is not used directly within a local loop or switch statement. It doesn't matter that you're creating your function within a loop; break and continue statements perform local control flow (within a function), not across functions.
In general, instead of using Future.then, you should prefer using async/await which will have the compiler generate the callback function with appropriate code transformations for you:
while (true) {
_changeRandomInt()
var documentSnapshot =
await firebaseFirestore.collection("myCollection").doc(_randomInt).get();
if (getFromFirebase(documentSnapshot) == null) {
break;
}
}
Note that performing an asynchronous operation continuously in a loop isn't very efficient, so you probably should consider alternative approaches if possible, however.
I am writing if statement like that:
String value;
if(int.parse(value)) // I want to write a condition that if error appear while parsing
// (like value contains some strings) then if statement run
It can be achieved by writing try/catch
try{
int.parse(value)
}
catch (e) {
// implement if statement
}
But I want to do this with if statement
Well you can try a different approach using tryParse() method and check whether the result is null then the parse failed.
if (int.tryParse(value)==null){ // execute failed parse code }
I am trying to use verify method of mockito using scala as a language. However for my case verify method is always successful, even if that method is not being called.
Below is the codeline where getDetailsBySkus calls method getPriceAndAvailability just once internally. How can I make this work ?
Below is the code snippet.
Code:
doReturn(Future(TestConstantsSpec.PNA_RESPONSE), Nil: _*)
.when(distributorService).getPriceAndAvailability(ArgumentMatchers.any())(ArgumentMatchers.any())
"empty part number" should "not execute calls-1" in {
purchaseOrderService.getDetailsBySkus(purchaseOrder.distributor, Seq("KJH"))
.flatMap(output => {
verify(distributorService, Mockito.atLeast(10))
.getPriceAndAvailability(ArgumentMatchers.any())(ArgumentMatchers.any()) // successful
Future(output)
}
)
}
"empty part number" should "not execute calls-2" in {
purchaseOrderService.getDetailsBySkus(purchaseOrder.distributor, Seq("KJH"))
.flatMap(output => {
verify(distributorService, Mockito.atLeast(100))
.getPriceAndAvailability(ArgumentMatchers.any())(ArgumentMatchers.any()) // successful
Future(output)
}
)
}
Your test cases are not waiting for the completion of the Futures, if you are using ScalaTest or similar, here's where you can read up on your options: https://www.scalatest.org/user_guide/async_testing
I have two Actions with the same input/output/error types, and I'd like to compose them into a single Action that runs whichever of the two is enabled (with an arbitrary tie-breaker if they both are).
Here's my first, failing, attempt:
let addOrRemove: Action<MyInput, MyOutput, APIRequestError> = Action(enabledIf: add.isEnabled.or(remove.isEnabled)) { input in
if add.isEnabled.value {
return add.apply(input)
} else {
return remove.apply(input)
}
}
This fails because the inner add.apply(input) can't see that I checked add.isEnabled, so it wraps an additional ActionError<> layer around the error type. (This might be legit, as I'm not sure how thread-safe this approach would be, or might be a case of us knowing something the type system doesn't.) The corresponding type error is:
cannot convert return expression of type 'SignalProducer<MyOutput, ActionError<APIRequestError>>' to return type 'SignalProducer<MyOutput, APIRequestError>'
What should I do instead?
Github user #ikesyo provided the following answer on the ReactiveSwift issue I opened to ask the same question:
let producer: SignalProducer<MyOutput, ActionError<APIRequestError>>
if add.isEnabled.value {
producer = add.apply(input)
} else {
producer = remove.apply(input)
}
return producer.flatMapError { error in
switch error {
case .disabled: return .empty
case let .producerFailed(inner): return SignalProducer(error: inner)
}
}
If they show up here I'll happily change the accepted answer to theirs (credit where it belongs).
Warning: If I'm reading this answer correctly, it's not watertight. If add changes from enabled to disabled between the apply() and the start() of the wrapping Action, we'll get "success" (no values, but .completed) instead of the .disabled we should get. That's good enough for my use case, but YMMV.
I tried to validate 2 conditions inside eventually block... something like this
eventually(timeout(Span(26, Seconds)), interval(Span(2, Seconds))) {
response = executeSomeFunction
response should be = (true)
if (response) {
something = responseResult.get
something should be >= (10)
}
}
What am looking for is eventually should satisfy both the conditions. That is first it should check if response is true and then when response is true, it should validate the condition inside if loop.
I tried executing this but am getting error message
ambiguous reference to overloaded definition" referencing to line
"response should be = (true)"
Am not sure what I am trying to do is even possible inside eventually or not.
The problem is that you write
response should be = (true)
But actually you want to write:
response shouldBe true
In your case you make assignment of response should be: ResultOfBeWordForAny[Boolean] to the value true. Not clear what conversion here you expect.
P.S. Also write response = executeSomeFunction outside of eventually block, otherwise it could be executed multiple times.
P.P.S Moreover you don't need eventual call if you test result of your function, it's anyway in the scope. eventually isn't the best practice and used when function have some async side-effects you would like to test.