How do i use tags in selenium tests using the Scala module for Playframework? - scala

I've read the docs on tags for the normal templates:
scala-0.9.1/templates#Tags
Now I'm trying to create a login-tag to be used in my selenium tests but I can't seem to get it to work.
In play for Java I have this:
# in file app/views/tags/test/loginAs.html
click('link=Log in')
type('css=.login input[name=email]', '${_email}')
type('css=.login input[name=password]', '${_password}')
clickAndWait('css=.login input[type=submit]')
In a test I use it like this:
#{test.loginAs email:'foo#bar.com', password:'1234' /}
Using Play with Scala I have tried this:
# in file app/views/tags/test/loginAs.scala.html
#(username:String, password: String)(body: (String) => Html)
click('link=Log in')
type('css=.login input[name=email]', '#email')
type('css=.login input[name=password]', '#password')
clickAndWait('css=.login input[type=submit]')
And in my test i first do the import:
import views.tags.html._
But when I call it from my test like so:
#loginAs("foo#bar.com", "1234")
I get no error, but no selenium code in my test either...
What I'm I doing wrong?
Edit
So, it turns out you don't have to use the Scala templates... I tried to use the "normal" templates like the one I described above and It works fine.

Related

How to use play-bootstrap in Twirl templates

I'm a play noob and I'm having an hard time trying to figure out which import(s) I need and where to put them in order to use play-bootstrap.
I've added the library as a dependency ("com.adrianhurt" %% "play-bootstrap" % "1.5.1-P27-B4"), then in my template, I'm trying to use:
#b4.vertical.form(routes.AccountController.handleSubscriptionForm) { implicit vfc =>
#helper.CSRF.formField
#b4.email(form("email"))
// ...and so on
}
But the template fail to compile with a weird error message:
Expected '"' but found 'EOF'
I'm sure I need somehow to import #b4 into the template context but I don't understand how.
PS. I'm able to use the "standard" play helpers without any issue, the following works perfectly:
#helper.form(routes.AccountController.handleSubscriptionForm) {
#helper.CSRF.formField
#helper.inputText(form("email"))
What I have to do in order to use the play bootstrap helpers?

Frontend headless browser testing using CasperJS and configuration-files?

I tried to use CasperJS for headless browser testing using PhantomJS and wanted to have a config file or something to change Website URL, Username passwords etc. So finally I found NuclearJS. Do you guys know any other perfect way to do this? If I wanted to write a one from the scratch would like to know about as well.
I got a solution (not perfect ;) ) that is using multiple configfiles (for selector, execution, desktop, mobile, etc).
I include a in the execution of my casperjs tests a file that offers me all configs i need (i include also global functions there).
Lets guess the test execution looks like that:
casperjs test --includes=loadGlobals.js test_1.js
In the that example loadGlobals.js contains functions like that:
var fs = require('fs');
var config = {},
configFile = fs.read('config.json');
config = JSON.parse(configFile);
Probalby the config.json is looking like that:
{
"url": "http://www.yourTestUrl.com",
"variable_1": "bla",
"variable_2": "blub",
"nextTier": {
"variable_1": "blablub"
}
}
Now you can call in the test_1.js the variables of the config.json:
casper.start(config.url, function() {
casper.then(function() {
casper.echo(config.variable_1);
casper.echo(config.variable_2);
casper.echo(config.nextTier.variable_1);
});
})
casper.run();
You can use like that different configurationfiles, even to override it during tests if nessacary.
The tests should be written in the page object pattern style so they are highly maintable, espacially with a outsourced configuration.
NuclearJS i didn't know, but i will take a look into it, too.

How to get PyTest fixtures to autocomplete in PyCharm (type hinting)

I had a bear of a time figuring this out, and it was really bugging me, so I thought I'd post this here in case anyone hit the same problem...
(and the answer is so dang simple it hurts :-)
The Problem
The core of the issue is that sometimes, not always, when dealing with fixtures in PyTest that return objects, when you use those fixtures in a test in PyCharm, you don't get autocomplete hints. If you have objects with large numbers of methods you want to reference while writing a test, this can add a lot of overhead and inconvenience to the test writing process.
Here's a simple example to illustrate the issue:
Let's say I've got a class "event_manager" that lives in:
location.game.events
Let's further say that in my conftest.py file (PyTest standard thing for the unfamiliar), I've got a fixture that returns an instance of that class:
from location.game.events import event_manager
...
#pytest.fixture(scope="module")
def event_mgr():
"""Creates a new instance of event generate for use in tests"""
return event_manager()
I've had issues sometimes, (but not always - I can't quite figure out why) with classes like this where autocomplete will not work properly in the test code where I use the fixture, e.g.
def test_tc10657(self, evt_mgr):
"""Generates a Regmod and expects filemod to be searchable on server"""
evt_mgr.(This does not offer autocomplete hints when you type ".")
So the answer is actually quite simple, once you review type hinting in PyCharm:
http://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html
Here's how to fix the above test code so that autocomplete works properly:
from location.game.events import event_manager
...
def test_tc10657(self, evt_mgr: event_manager):
"""Generates a Regmod and expects filemod to be searchable on server"""
evt_mgr.(This DOES offer hints when you type "." Yay!)
Notice how I explicitly type the fixture as an input parameter of type event_manager.
Also if you add a docstring to a function and specify the type of the the parameters, you will get the code completion for those parameters.
For example using pytest and Selenium:
# The remote webdriver seems to be the base class for the other webdrivers
from selenium.webdriver.remote.webdriver import WebDriver
def test_url(url, browser_driver):
"""
This method is used to see if IBM is in the URL title
:param WebDriver browser_driver: The browser's driver
:param str url: the URL to test
"""
browser_driver.get(url)
assert "IBM" in browser_driver.title
Here's my conftest.py file as well
import pytest
from selenium import webdriver
# Method to handle the command line arguments for pytest
def pytest_addoption(parser):
parser.addoption("--driver", action="store", default="chrome", help="Type in browser type")
parser.addoption("--url", action="store", default='https://www.ibm.com', help="url")
#pytest.fixture(scope='module', autouse=True)
def browser_driver(request):
browser = request.config.getoption("--driver").lower()
# yield the driver to the specified browser
if browser == "chrome":
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
else:
raise Exception("No driver for browser " + browser)
yield driver
driver.quit()
#pytest.fixture(scope="module")
def url(request):
return request.config.getoption("--url")
Tested using Python 2.7 and PyCharm 2017.1. The docstring format is reStructuredText and the "Analyze Python code in docstrings" checkbox is checked in settings.

How to render a scalate template manually?

I want to try Scalate in this way:
Provide a scalate template, say: index.html
Use scala code to render it with some data manually
Any template format is OK(mustache, Scaml, SSP, Jade)
But I sad found nothing to do this even if I have read all the documentation and source code I found.
In order to make this question more clear, so I have such a template user.html:
<%# var user: User %>
<p>Hi ${user.name},</p>
#for (i <- 1 to 3)
<p>${i}</p>
#end
<p>See, I can count!</p>
I want to render it with a user instance User(name="Mike"). How to do it?
Suppose you have the following simple_example.mustache template:
I like {{programming_language}}
The code is {{code_description}}
You can render the template with this code:
import org.fusesource.scalate.TemplateEngine
val sourceDataPath = os.pwd/"simple_example.mustache".toString
val engine = new TemplateEngine
val someAttributes = Map(
"programming_language" -> "Scala",
"code_description" -> "pretty"
)
engine.layout(sourceDataPath, someAttributes)
Here's the result:
I like Scala
The code is pretty
Once you get past the initial learning hump, Scalate is actually pretty nice to work with (the docs don't make the lib easy to use).

#javascript cucumber tests pass using selenium driver but fail when using poltergiest

I'm trying to test an jquery UI autocomplete, I've got the tests passing using the selenium driver. I want to switch to poltergiest for some headless testing, but now my tests are now failing.
It doesn't seem to select the autocomplete option for some reason that I have yet been able to figure out
Step
When /^select contract$/ do
VCR.use_cassette("contract") do
selector =
'.ui-menu-item a:contains("John Smith (123456)")'
within("div#review") do
fill_in("contract", with: "john")
end
sleep 2
page.execute_script "$('#{selector}').trigger(\"mouseenter\").click();"
within("div#myPerformaceReview") do
find_field("contract").value.should ==
"John Smith (123456)"
end
end
end
The test passes using the Selenium driver without any changes to the step.
Any advice on how I could debug this?
Version
selenium-webdriver (2.27.2)
poltergeist (1.0.2)
cucumber (1.2.1)
cucumber-rails (1.0.6)
capybara (1.1.4)
phantomjs 1.8.1
I've managed to figure it out, it seems capybara-poltergeist driver doesn't trigger any of the events that jquery-ui uses to display the dropdown list.
I found the answer here: https://github.com/thoughtbot/capybara-webkit/issues/50
I created a form helper in features/support
module FormHelper
def fill_in_autocomplete(selector, value)
page.execute_script %Q{$('#{selector}').val('#{value}').keydown()}
end
def choose_autocomplete(text)
find('ul.ui-autocomplete').should have_content(text)
page.execute_script("$('.ui-menu-item:contains(\"#{text}\")').find('a').trigger('mouseenter').click()")
end
end
World(FormHelper)
I then used those method to fill in the form and select the desired option.
Martin's answer almost worked for me, but I found that the input needs to be focused as well to make it work:
module FormHelper
def fill_in_autocomplete(selector, value)
page.execute_script %Q{$('#{selector}').focus().val('#{value}').keydown()}
end
def choose_autocomplete(text)
find('ul.ui-autocomplete').should have_content(text)
page.execute_script("$('.ui-menu-item:contains(\"#{text}\")').find('a').trigger('mouseenter').click()")
end
end
Found this on the same page: https://github.com/thoughtbot/capybara-webkit/issues/50#issuecomment-4978108