Sharing selectors between Selenium and Galen tests

753 views
Skip to first unread message

jos...@gmail.com

unread,
Feb 26, 2016, 3:21:02 PM2/26/16
to Galen Framework
I have recently learned about Galen Framework and I'm trying to add it into my already existing Java/TestNG/Selenium tests. However, I can't find any examples of using already defined selectors from Selenium page objects in the Galen ".gspec" file. Every example I have found is redefining them directly in the ".gspec" like so:

@objects
    header #header
    footer   #footer

My issue is that I don't want to be defining these selectors in two locations, because that doubles the work of maintaining/adding selectors down the road, so is it possible to share them between Selenium and Galen tests?

Thanks in advance!


Ivan Shubin

unread,
Feb 26, 2016, 4:14:48 PM2/26/16
to Galen Framework
Hi,

I don't have any examples for that at the moment but it is possible. Here is the method that allows this https://github.com/galenframework/galen/blob/8f878e98a71394e13149b00ee3d44b7dbb61fe61/galen-core/src/main/java/com/galenframework/api/Galen.java#L62 

You just need to pass a map of objects (which is just pairs of object names and locators). Here is the locator class http://jdoc.galenframework.com/com/galenframework/specs/page/Locator.html

Oh yeah, and instead of driver you need to pass a Browser object. So then you can wrap your driver into new SeleniumBrowser(driver)

Regards,
Ivan

Josh Diaz

unread,
Feb 29, 2016, 3:17:40 PM2/29/16
to Galen Framework
Thanks Ivan. I have changed my code to utilize that method, however for some reason my tests always pass, even when the expected conditions are not correct. Below is the code that I am using to execute the ".gspec" file:

@Test
void CheckMobileLayout()
{
checkLayout("mobile")
}

private void checkLayout(String platform)
{
Properties properties = new Properties()
properties.load(new FileInputStream(getTestResource("test.properties")))

load(WebDriverRunner.webDriver.currentUrl)

Browser browser = new SeleniumBrowser(WebDriverRunner.webDriver)
browser.changeWindowSize(platform == "desktop" ? new Dimension(1024, 750) : new Dimension(300, 700))

Galen.checkLayout(browser,
specPath,
new SectionFilter(asList(platform, "all"), Collections.emptyList()),
properties,
Collections.emptyMap(),
null,
null,
locators)
}


and the contents of my ".gspec" are as follows:

= Landing Page =
@on *
mainTitle:
text is "DevelopingScientists"
above mainTitleDescription

mainTitleDescription:
below mainTitle
above educationButton

educationButton:
css background-color is ${BRIGHT_GREEN}

discoveryButton:
css background-color is ${GREEN}

leadershipButton:
css background-color is ${GREEN}

@on desktop
educationButton:
left-of discoveryButton

discoveryButton:
right-of educationButton
left-of leadershipButton

leadershipButton:
right-of discoveryButton

@on mobile
educationButton:
above discoveryButton

discoveryButton:
below educationButton
above leadershipButton

leadershipButton:
above discoveryButton

I have also tried adding "Landing Page" into the call to the SectionFilter constructor, however that did not help either. Not sure where I'm going wrong. Could you please help guide me in the right direction here? The line that should fail is when checking that "leadershipButton" has a css background-color of ${GREEN}, however it passes every time. I've also tried changing many of the other expectations on my ".gspec" but no matter what I do, it always passes.

Ivan Shubin

unread,
Feb 29, 2016, 3:29:31 PM2/29/16
to Galen Framework
Hi,

Galen.checkLayout never fails. It doesn't throw layout validation related exceptions. The only exception it could throw - FileSyntaxException in case there is a syntax error in your specs. But it returns a layout report and that is what you need. I would suggest you to take a look at the implementation of TestNG + Galen Java tests in this project https://github.com/galenframework/galen-sample-java-tests
That project uses galen-java-support lib, which is released together with galen-core. By the way here is how it uses LayoutReport https://github.com/galenframework/galen/blob/master/galen-java-support/src/main/java/com/galenframework/support/GalenJavaTestBase.java#L117

Kind regards,
Ivan

Josh Diaz

unread,
Mar 3, 2016, 11:33:09 AM3/3/16
to Galen Framework
Thanks Ivan! That worked for me.

Marthijs

unread,
Jun 17, 2016, 6:28:49 AM6/17/16
to Galen Framework
Hello Ivan,

I've implemented a similar logic for my java framework and now I would like to declare nested objects and multiple objects definitions,  what manipulation should I apply to the Locator to define it's nesting and how can I define a locator as multiple objects?

Ivan Shubin

unread,
Jun 22, 2016, 6:38:06 AM6/22/16
to Galen Framework
Hi Marthijs,

Take a look at this method "withParent" https://github.com/galenframework/galen/blob/master/galen-core/src/main/java/com/galenframework/specs/page/Locator.java#L146. You can specify a parent to each locator so that you can build a chain of locators bottom up.

Regards,
Ivan

nikol...@gmail.com

unread,
Oct 27, 2016, 10:09:28 AM10/27/16
to Galen Framework
Hi Josh,

Do you mind sharing the config file for running galen test with java please?
Did you update the java test?

I am trying to run a .gspec file using java testNg.
I would like to run it on 3 devices [mobile, tablet, desktop]

I can run it ok on comand line using galen .test file have not figured out how to run with parameters using java testng.
.test file

@@ Set
page_name it-it/
page_url http://url/${page_name}
page_spec_url c:/***/***/homepage_IT.gspec
@@ Parameterized
| viewport | size |
| mobile | 480x640 |
| tablet | 768x1024 |
| desktop | 1366x768 |
${page_name} ${viewport} viewport ${full_name}
${page_url} ${size}
check ${page_spec_url} --include "${viewport}"


Thank you
Niko

nikol...@gmail.com

unread,
Oct 27, 2016, 10:37:23 AM10/27/16
to Galen Framework
Hi Ivan,

I am trying to run galen test using Java ... GalenTestNgTestBase since yesterday

If I use a .gspec file with all object and spec defined in one file it works
But if I use @import to import other .jspec it does not work, no tests are run, and no exception. Heat map image is shown [no heat zones/locators] but no test/checks are run

on my main .gspec file I have this:

#
@import modules/homepage_header_it.gspec


Is the import feature supported using Java ?

The file runs fine if I use a .test file and run it on CMD.


Thanks
Niko



Josh Diaz

unread,
Oct 27, 2016, 11:32:39 AM10/27/16
to Galen Framework
I created this method which I think you will find useful. It is written in Groovy, not Java however. You need to put this method in your test base class that you created, which should extend GalenTestNgTestBase, and execute tests by sending parameters to it for each specific spec file or configuration you want (i.e. mobile, tablet, desktop, etc):

protected void executeGalenSpec(List<String> tags, String specPath, Closure doSomething)
{
tags = new LinkedList<>(tags)
tags << browser

Properties properties = new Properties()
properties.load(new FileInputStream(getTestResource("test.properties")))

load(WebDriverRunner.webDriver.currentUrl)

    if (doSomething != null)
{
doSomething()

}

Browser browser = new SeleniumBrowser(WebDriverRunner.webDriver)
    browser.changeWindowSize(tags.contains("desktop") ? new java.awt.Dimension(1200, 700) : new java.awt.Dimension(300, 700))

SectionFilter sectionFilter = new SectionFilter(tags, [] as List)

LayoutReport layoutReport = Galen.checkLayout(browser,
specPath,
sectionFilter,
properties,
Collections.emptyMap(),
null,
null,
galenLocators)

getReport().layout(layoutReport, "Checking " + this.class.getName() + " for tags: " + tags.each { it + "," })

if (layoutReport.errors() > 0)
{
throw new LayoutValidationException(specPath, layoutReport, sectionFilter)
}
}

This can then run a test like below:

@Test
void CheckMobileLayout()
{
executeGalenSpec(["mobile"], "/path/to/file.gspec", { /* a closure with any action that must be performed in the browser before executing your tests using selenium or some other framework */ })
}

The test.properties file looks likse this:

# Params for page A
BRIGHT_GREEN="rgb(119, 187, 78)"
GREEN="rgb(0, 166, 75)"
TEAL="rgb(0, 141, 152)"
ORANGE="rgb(228, 112, 0)"
LIGHT_GRAY="rgb(232, 232, 232)"
MEDIUM_GRAY="rgb(119, 119, 119)"
DARK_GRAY="rgb(83, 85, 84)"
BLACK="rgb(0, 0, 0)"
BLUE="rgb(0, 72, 140)"
WHITE="rgb(255, 255, 255)"

# Params for page B
SECTION1_TITLE="TITLE 1"
SECTION2_TITLE="TITLE 2"
SECTION3_TITLE="TITLE 3"
SECTION4_TITLE="TITLE 4"


I am able to reference any of those params from test.properties using this setup, by just calling ${param}. Let me know if that answers all of your questions or if you need more help.

Josh Diaz

unread,
Oct 27, 2016, 11:45:32 AM10/27/16
to Galen Framework
Also forgot to mention, the "galenLocators" param, is of type Map<String, com.galenframework.specs.page.Locator>.

nikol...@gmail.com

unread,
Oct 28, 2016, 7:29:08 AM10/28/16
to Galen Framework
Hi Josh,

I have a question Regarding galenLocators parameter. 
This is defined in .gspec file that is passed to method as specPath param.

Do I need to setup the Map for galenLocators manually?
Does Galen has some helper methods for this ?


Thanks
Niko

Josh Diaz

unread,
Oct 28, 2016, 3:55:06 PM10/28/16
to Galen Framework
I just created the map in my Groovy code. Basically in each PageObject for my Selenium tests, I have a static method called getGalenLocators() and that method populates the galenLocators map with all of the relevant locators. Here is an example in Java:

galenLocators.put("title", Locator.css("#title"));
galenLocators.put("summary", Locator.css("#summary"));
galenLocators.put("okButton", Locator.css("#okButton"));

Once you've added all of your selectors to this map and passed it to the Galen.checkLayout() function, you can reference each element on the page in your .gspec file by simply using the key assigned in the map. So for example, using the locators I've given above, you could say:

title:
    text is "This is the expected title"


yeshu.a...@gmail.com

unread,
Feb 15, 2018, 2:03:30 AM2/15/18
to Galen Framework


Hi  Ivan,

I have used the similar logic in my project the check layout as you have mentioned showed pass but the Galen report does not show it as fail. Kindly help me in this regard


yeshu.a...@gmail.com

unread,
Feb 15, 2018, 2:16:37 AM2/15/18
to Galen Framework
the spec file used is as follows :


I have modified the spec file intentionally to check if the login button is above the password field. Even though the button is displayed below the password field the report does not show it as fail. Request your help in this regard.


Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages