{"id":2109,"date":"2015-09-16T09:06:05","date_gmt":"2015-09-16T07:06:05","guid":{"rendered":"http:\/\/hasselba.ch\/blog\/?p=2109"},"modified":"2015-09-16T12:02:24","modified_gmt":"2015-09-16T10:02:24","slug":"testing-xpages","status":"publish","type":"post","link":"https:\/\/hasselba.ch\/blog\/?p=2109","title":{"rendered":"Testing XPages"},"content":{"rendered":"<p>When testing XPages with <a href=\"http:\/\/www.seleniumhq.org\/\" target=\"_blank\">Selenium<\/a>, you can easily pre-generate the JUnit test code\u00a0with the browser plugin. But when you then\u00a0change the structure of the XPage (f.e. by moving the components from an XPage to a custom control), all the IDs of the JUnit test will not work anymore.<\/p>\n<p>That&#8217;s why it is better to use CSS selectors to access the generated fields:<\/p>\n<pre><code>driver.findElements(By.cssSelector(\"input[id*='idOfTheComponent']\"));<\/code><\/pre>\n<p>With the selector\u00a0<em>&#8222;id*=&#8217;idOfTheComponent'&#8220;<\/em> you can access the elements by their component id, idependently of their full generated client id.<\/p>\n<p>Here is an example with a small XPage with a radio group and a listbox:<\/p>\n<p>&nbsp;<\/p>\n<h2>A simple XPage to test (SimpleDemo.xsp)<\/h2>\n<pre>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;xp:view xmlns:xp=\"http:\/\/www.ibm.com\/xsp\/core\"&gt;\r\n\r\n    &lt;xp:radioGroup\r\n        id=\"radioGroupSimpleDemo\"\r\n        defaultValue=\"1\"&gt;\r\n        &lt;xp:selectItem itemLabel=\"1\" itemValue=\"1\" \/&gt;\r\n        &lt;xp:selectItem itemLabel=\"2\" itemValue=\"2\" \/&gt;\r\n    &lt;\/xp:radioGroup&gt;\r\n\r\n\r\n    &lt;xp:listBox id=\"listBoxSimpleDemo\" multiple=\"true\"&gt;\r\n        &lt;xp:selectItem itemLabel=\"1\" itemValue=\"1\" \/&gt;\r\n        &lt;xp:selectItem itemLabel=\"2\" itemValue=\"2\" \/&gt;\r\n        &lt;xp:selectItem itemLabel=\"3\" itemValue=\"3\" \/&gt;\r\n    &lt;\/xp:listBox&gt;\r\n \r\n&lt;\/xp:view&gt;<\/pre>\n<h2>\u00a0The JUnit Test<\/h2>\n<pre><code>package ch.hasselba.xpages.test.seleniumdemo;\r\n\r\nimport static org.junit.Assert.assertFalse;\r\nimport static org.junit.Assert.assertTrue;\r\nimport static org.junit.Assert.fail;\r\n\r\nimport java.util.List;\r\nimport java.util.concurrent.TimeUnit;\r\n\r\nimport org.junit.After;\r\nimport org.junit.Before;\r\nimport org.junit.Test;\r\nimport org.openqa.selenium.By;\r\nimport org.openqa.selenium.WebDriver;\r\nimport org.openqa.selenium.WebElement;\r\nimport org.openqa.selenium.firefox.FirefoxDriver;\r\nimport org.openqa.selenium.support.ui.Select;\r\n\r\npublic class SimpleDemo {\r\n\r\n\tprivate WebDriver driver;\r\n\tprivate String baseUrl = \"http:\/\/127.0.0.1\/WebTestDemo.nsf\/SimpleDemo.xsp\";\r\n\tprivate StringBuffer verificationErrors = new StringBuffer();\r\n\r\n\t@Before\r\n\tpublic void setUp() throws Exception {\r\n\t\tdriver = new FirefoxDriver();\r\n\t\tdriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);\r\n\t}\r\n\r\n\t@After\r\n\tpublic void tearDown() throws Exception {\r\n\t\tdriver.quit();\r\n\t\tString verificationErrorString = verificationErrors.toString();\r\n\t\tif (!\"\".equals(verificationErrorString)) {\r\n\t\t\tfail(verificationErrorString);\r\n\t\t}\r\n\t}\r\n\r\n\t@Test\r\n\tpublic void testDemo() throws Exception {\r\n\t\treloadPage();\r\n\t\tList elemRadio = driver.findElements(By\r\n\t\t\t\t.cssSelector(\"input[id*='radioGroupSimpleDemo']\"));\r\n\t\tassertTrue(elemRadio.get(0).isSelected());\r\n\t\tassertFalse(elemRadio.get(1).isSelected());\r\n\r\n\t\tSelect select = new Select(driver.findElement(By\r\n\t\t\t\t.cssSelector(\"select[id*='listBoxSimpleDemo']\")));\r\n\t\tList listSelect = select.getOptions();\r\n\t\tfor (WebElement listElem : listSelect) {\r\n\t\t\tassertFalse(listElem.isSelected());\r\n\t\t\tassertTrue(listElem.isEnabled());\r\n\t\t}\r\n\r\n\t}\r\n\r\n\tpublic void reloadPage() {\r\n\t\tdriver.get(baseUrl);\r\n\t}\r\n}<\/code><\/pre>\n<h2>The Maven <em>pom.xml<\/em> file<\/h2>\n<pre><code>&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"\r\n    xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n    xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0\r\n    http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\r\n\r\n    &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n    &lt;groupId&gt;ch.hasselba.xpages.test&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;seleniumdemo&lt;\/artifactId&gt;\r\n    &lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\r\n    &lt;packaging&gt;jar&lt;\/packaging&gt;\r\n    &lt;name&gt;seleniumdemo&lt;\/name&gt;\r\n    &lt;url&gt;http:\/\/hasselba.ch&lt;\/url&gt;\r\n\r\n    &lt;properties&gt;\r\n        &lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\r\n    &lt;\/properties&gt;\r\n\r\n    &lt;dependencies&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;junit&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;junit&lt;\/artifactId&gt;\r\n            &lt;version&gt;4.12&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;org.seleniumhq.selenium&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;selenium-java&lt;\/artifactId&gt;\r\n            &lt;version&gt;2.46.0&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;org.seleniumhq.selenium&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;selenium-api&lt;\/artifactId&gt;\r\n            &lt;version&gt;2.46.0&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;org.hamcrest&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;hamcrest-core&lt;\/artifactId&gt;\r\n            &lt;version&gt;1.3&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;pl.pragmatists&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;JUnitParams&lt;\/artifactId&gt;\r\n            &lt;version&gt;1.0.4&lt;\/version&gt;\r\n            &lt;scope&gt;test&lt;\/scope&gt;\r\n        &lt;\/dependency&gt;\r\n    &lt;\/dependencies&gt;\r\n&lt;\/project&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When testing XPages with Selenium, you can easily pre-generate the JUnit test code\u00a0with the browser plugin. But when you then\u00a0change the structure of the XPage (f.e. by moving the components from an XPage to a custom control), all the IDs &hellip; <a href=\"https:\/\/hasselba.ch\/blog\/?p=2109\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,89,81,74],"tags":[47,7,31,106,107,108,12,3],"class_list":["post-2109","post","type-post","status-publish","format-standard","hentry","category-allgemein","category-java","category-web","category-xpages","tag-9-0","tag-domino","tag-java","tag-junit","tag-selenium","tag-testing","tag-web","tag-xpages"],"_links":{"self":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2109"}],"version-history":[{"count":4,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2109\/revisions"}],"predecessor-version":[{"id":2135,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2109\/revisions\/2135"}],"wp:attachment":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}