lefthandedgoat / canopy

f# web automation and testing library, built on top of Selenium (friendly to c# also)
http://lefthandedgoat.github.io/canopy/
MIT License
506 stars 115 forks source link

How to return integer value from function which contains multiple lines code #469

Closed RutujaNagare closed 5 years ago

RutujaNagare commented 5 years ago

Hi,

I want to return an integer value from a function having below code.

// Function definition
let VerifyNotesFromDealDetails(stage : string, approver : string) =
    "Verify notes for Approval required " &&& fun _ ->
    let flag = 0
    let arrStage = stage.Split[|'^'|]
    let arrApprover = approver.Split[|'^'|]
    let count = arrApprover.Length
    let listCount = [1..count]

    for i in listCount do
        let expectedNotes = "Stage gate at "+arrStage.[i-1]+" until approved by - "+arrApprover.[i-1]
        let tempLocator = ".//*[@ng-repeat='appr in vm.deal.ActiveProposal.Approvals']["+(i).ToString()+"]"
        let actualNotes = read tempLocator
        if expectedNotes.contains(actualNotes) = true then
            flag = 1            // i want to return this flag value out side the loop.
       else
            flag = 0

Could you please help how to write return data type to the function and how to return this flag value from function.

I tried to write return data type after parameters but receiving error "This expression was expected to have type 'int' but here has type 'unit'"

lefthandedgoat commented 5 years ago

change flag to let mutable flag = 0

and make flag the last line of your function to make it the return line.

RutujaNagare commented 5 years ago

Hi,

I tried the solution. but I want to return a flag value outside the for loop. So is tried below code.

let VerifyNotesFromDealDetails(stage : string, approver : string) =
    "Verify notes for Approval required " &&& fun _ ->
    let mutable flag = 0
    let arrStage = stage.Split[|'^'|]
    let arrApprover = approver.Split[|'^'|]
    let count = arrApprover.Length
    let listCount = [1..count]

    for i in listCount do
        let expectedNotes = "Stage gate at "+arrStage.[i-1]+" until approved by - "+arrApprover.[i-1]
        let tempLocator = ".//*[@ng-repeat='appr in vm.deal.ActiveProposal.Approvals']["+(i).ToString()+"]"
        let actualNotes = read tempLocator
        if expectedNotes.Contains(actualNotes) = true then
            flag <- flag + 1    
    flag      

But it is displaying error as 'This expression was expected to have type 'Unit' but here has type 'int'.

Also at the calling point of this function I received NULLValueException

let temp = VerifyNotesFromDealDetails("Proposal^Executed","Rutuja Nagare^Rutuja Nagare")
        if temp.ToString().Contains("0") then
            TestContext.Progress.WriteLine("Notes on Deal Details are incorrect")
        else
            TestContext.Progress.WriteLine("Notes on Deal Details are incorrect")

Could you please help..

lefthandedgoat commented 5 years ago

You've defined a canopy test inside, that is probably the problem

"Verify notes for Approval required " &&& fun _ ->

RutujaNagare commented 5 years ago

Can you please suggest how do I collect return value at the calling point of the function? My Function call is let temp = VerifyNotesFromDealDetails("Proposal^Executed","Rutuja Nagare^Rutuja Nagare") if temp.ToString().Contains("0") then TestContext.Progress.WriteLine("Notes on Deal Details are incorrect") else TestContext.Progress.WriteLine("Notes on Deal Details are incorrect")

There I am getting NULLValueException while running the test

lefthandedgoat commented 5 years ago

I don't understand your question, but it looks like you could get a null reference from temp being null. I dont know what VerifyNotiesFromDealDetails does.

Either way, this is a general F# programming question, not a canopy one. There are plenty of good F# programming books out there, including some free ones.

https://fsharpforfunandprofit.com/

amirrajan commented 5 years ago

This is unrelated to canopy.