Haskell withSubprocess construct around do block

I am working on the selenium Haskell WebDriver package for testing, here .

I have this example:

import Test.WebDriver firefoxConfig :: WDConfig firefoxConfig = defaultConfig main :: IO () main = runSession firefoxConfig $ do openPage "http://google.com" searchInput <- findElem ( ByCSS "input[type='text']" ) sendKeys "Hello, World!" searchInput submit searchInput closeSession 

getting started makes it clear that the selenium client needs the selenium server to communicate with

 java -jar selenium-server-standalone-*.jar 

Without launch, you will get the following:

 ghci λ> main *** Exception: FailedConnectionException2 "127.0.0.1" 4444 False connect: does not exist (Connection refused) 

I would like to complete the entire test script in a function that initializes the selenium server, writes its pid and kill (pid) after starting the session. That is, throughout my existing core, I would like to name java selenium-server in existence, but I would like it to cease to exist as soon as the call ends.

In python, I would do this using the definition of __enter__() and __exit__() with other dump files, subprocess.Popen , write down the id, kill it, and then call

 with Browser() as b: do_stuff 

It seems to me that the essence of runSession is what I would need to replicate in order to wrap the launch and stall like this in the sense that it takes the firefoxConfig $ do block as an argument, and I also want to do this.

However, I cannot fully understand the runSession polling types, how to do this:

 ghci λ> :t runSession runSession :: Test.WebDriver.Config.WebDriverConfig conf => conf -> WD a -> IO a 

I think I would be looking for some kind of Monad method that I could apply to this that applies to do . I think the syntax will be kind ...

 import Test.WebDriver import System.Process firefoxConfig :: WDConfig firefoxConfig = defaultConfig withBrowser :: Monad a -> Monad a -- maybe this type? withBrowser = do r <- createProcess (proc "java -jar selenium-server-standalone-*.jar" []) -- other magic here? main :: IO () main = withBrowser $ runSession firefoxConfig $ do openPage "http://google.com" searchInput <- findElem ( ByCSS "input[type='text']" ) sendKeys "Hello, World!" searchInput submit searchInput closeSession 

How can i achieve this? Monad right? Is there a more or more Haskill idiom or strategy for this?

+6
source share
1 answer

You just want a bracket from https://hackage.haskell.org/package/base-4.9.0.0/docs/Control-Exception.html#v:bracket .

It allows you to specify I / O operations for installation and uninstallation to complete the third step. It automatically processes the output of the installation action in both basic and tearing actions, whether your installation action should simply indicate the PID as it occurs, therefore, as a result of the teardown action, it will be indicated that the PID will kill.

Sort of:

 withBrowser browserAction = bracket startSelenium killSelenium (const browserAction) 

(Where I suggested that you do not want the main action to take an argument for pid, so I used const to ignore it)

+1
source

Source: https://habr.com/ru/post/1012988/


All Articles