Best FluentLenium code snippet using org.fluentlenium.core.url.UrlTemplateTest.getParameterNames
Source:UrlTemplateTest.java
...17 @Test18 public void testOnlyParameter() {19 UrlTemplate urlParametersTemplate = new UrlTemplate("{param1}");20 String url = urlParametersTemplate.add("test1").render();21 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1");22 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false);23 assertThat(url).isEqualTo("test1");24 }25 @Test26 public void testOnlyOptionalParameter() {27 UrlTemplate urlParametersTemplate = new UrlTemplate("{?param1}");28 String url = urlParametersTemplate.add("test1").render();29 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1");30 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(true);31 assertThat(url).isEqualTo("test1");32 }33 @Test34 public void testRender() {35 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def/{param2}/{param3}");36 String url = urlParametersTemplate.add("test1").add("test2").add("test3").render();37 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");38 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, false, false);39 assertThat(url).isEqualTo("/abc/test1/def/test2/test3");40 }41 @Test42 public void testRenderOptionalParameter() {43 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def{?/param2}/ghi{?/param3}");44 String url = urlParametersTemplate.add("test1").add("test2").render();45 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");46 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, true, true);47 assertThat(url).isEqualTo("/abc/test1/def/test2/ghi");48 urlParametersTemplate.clear();49 assertThatThrownBy(urlParametersTemplate::render).isInstanceOf(IllegalArgumentException.class)50 .hasMessageContaining("Value for parameter param1 is missing");51 }52 @Test53 public void testRenderNullOptionalParameter() {54 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def{?/param2}/ghi{?/param3}");55 String url = urlParametersTemplate.add("test1").add(null).add("test3").render();56 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");57 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, true, true);58 assertThat(url).isEqualTo("/abc/test1/def/ghi/test3");59 }60 @Test61 public void testRenderNullOptionalPathParameter() {62 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}{?/def/param2}{?/ghi/param3}");63 String url = urlParametersTemplate.add("test1").add(null).add("test3").render();64 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");65 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, true, true);66 assertThat(url).isEqualTo("/abc/test1/ghi/test3");67 }68 @Test69 public void testParse() {70 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def/{param2}/{param3}");71 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");72 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, false, false);73 ParsedUrlTemplate parsed = urlParametersTemplate.parse("/abc/v1/def/v2/v3");74 assertThat(parsed.matches()).isTrue();75 assertThat(parsed.parameters()).hasSize(3);76 assertThat(parsed.parameters().keySet()).containsExactly("param1", "param2", "param3");77 assertThat(parsed.parameters().values()).containsExactly("v1", "v2", "v3");78 }79 @Test80 public void testParseWithEndingSlash() {81 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def/{param2}/{param3}/");82 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");83 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, false, false);84 ParsedUrlTemplate parsed = urlParametersTemplate.parse("/abc/v1/def/v2/v3");85 assertThat(parsed.matches()).isTrue();86 assertThat(parsed.parameters()).hasSize(3);87 assertThat(parsed.parameters().keySet()).containsExactly("param1", "param2", "param3");88 assertThat(parsed.parameters().values()).containsExactly("v1", "v2", "v3");89 }90 @Test91 public void shouldThrowExceptionIfUriHasInvalidSyntax() {92 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def/{param2}/{param3}/");93 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");94 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, false, false);95 assertThatIllegalArgumentException()96 .isThrownBy(() -> urlParametersTemplate.parse("/abc /v1/def/v2/v3"))97 .withCauseInstanceOf(URISyntaxException.class);98 }99 @Test100 public void testParseOptionalParameter() {101 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def/{param2}{?/param3}");102 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");103 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, false, true);104 ParsedUrlTemplate parsed = urlParametersTemplate.parse("/abc/v1/def/v2");105 assertThat(parsed.matches()).isTrue();106 assertThat(parsed.parameters()).hasSize(2);107 assertThat(parsed.parameters().keySet()).containsExactly("param1", "param2");108 assertThat(parsed.parameters().values()).containsExactly("v1", "v2");109 }110 @Test111 public void testParseOptionalPathParameter() {112 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}{?/def/param2}{?/param3}");113 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");114 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, true, true);115 ParsedUrlTemplate parsed = urlParametersTemplate.parse("/abc/v1/def/v2");116 assertThat(parsed.matches()).isTrue();117 assertThat(parsed.parameters()).hasSize(2);118 assertThat(parsed.parameters().keySet()).containsExactly("param1", "param2");119 assertThat(parsed.parameters().values()).containsExactly("v1", "v2");120 }121 @Test122 public void testParseOptionalMiddleParameter() {123 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def{?/param2}/ghi/{param3}");124 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");125 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, true, false);126 ParsedUrlTemplate parsed = urlParametersTemplate.parse("/abc/v1/def/ghi/v3");127 assertThat(parsed.matches()).isTrue();128 assertThat(parsed.parameters()).hasSize(2);129 assertThat(parsed.parameters().keySet()).containsExactly("param1", "param3");130 assertThat(parsed.parameters().values()).containsExactly("v1", "v3");131 }132 @Test133 public void testParseOptionalPathMiddleParameter() {134 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}{?/def/param2}{/ghi/param3}");135 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");136 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, true, false);137 ParsedUrlTemplate parsed = urlParametersTemplate.parse("/abc/v1/ghi/v3");138 assertThat(parsed.matches()).isTrue();139 assertThat(parsed.parameters()).hasSize(2);140 assertThat(parsed.parameters().keySet()).containsExactly("param1", "param3");141 assertThat(parsed.parameters().values()).containsExactly("v1", "v3");142 }143 @Test144 public void testParseParametersFromQueryParameters() {145 UrlTemplate urlParametersTemplate = new UrlTemplate("?param1={param1}¶m2={param2}");146 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2");147 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, false);148 ParsedUrlTemplate parsed = urlParametersTemplate.parse("?param1=v1¶m2=v2");149 assertThat(parsed.matches()).isTrue();150 assertThat(parsed.parameters()).hasSize(2);151 assertThat(parsed.parameters().keySet()).containsExactly("param1", "param2");152 assertThat(parsed.parameters().values()).containsExactly("v1", "v2");153 }154 @Test155 public void testParseParametersFromPathAndQueryParameters() {156 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}?param2={param2}¶m3={param3}");157 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");158 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, false, false);159 ParsedUrlTemplate parsed = urlParametersTemplate.parse("/abc/v1?param2=v2¶m3=v3");160 assertThat(parsed.matches()).isTrue();161 assertThat(parsed.parameters()).hasSize(3);162 assertThat(parsed.parameters().keySet()).containsExactly("param1", "param2", "param3");163 assertThat(parsed.parameters().values()).containsExactly("v1", "v2", "v3");164 }165 @Test166 public void testParseNotMatchingOptionalMiddleParameter() {167 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def{?/param2}/ghi/{param3}");168 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");169 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, true, false);170 ParsedUrlTemplate parsed = urlParametersTemplate.parse("/abc/v1/def/ghi");171 assertThat(parsed.matches()).isFalse();172 assertThat(parsed.parameters()).isEmpty();173 }174 @Test175 public void testParseNotMatchingUrl() {176 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def/{param2}{?/param3}");177 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");178 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, false, true);179 ParsedUrlTemplate parsed = urlParametersTemplate.parse("/abc/v1/abc/v2");180 assertThat(parsed.matches()).isFalse();181 assertThat(parsed.parameters()).isEmpty();182 }183 @Test184 public void testParseNotMatchingStartingUrl() {185 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def/{param2}{?/param3}");186 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");187 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, false, true);188 ParsedUrlTemplate parsed = urlParametersTemplate.parse("/abc/v1/def/v2/v3/ghi");189 assertThat(parsed.matches()).isFalse();190 assertThat(parsed.parameters()).isEmpty();191 }192 @Test193 public void testParseMatchingWithTrailingSlash() {194 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def/{param2}{?/param3}");195 assertThat(getParameterNames(urlParametersTemplate)).containsExactly("param1", "param2", "param3");196 assertThat(getParameterOptionals(urlParametersTemplate)).containsExactly(false, false, true);197 ParsedUrlTemplate parsed = urlParametersTemplate.parse("/abc/v1/def/v2/v3/");198 assertThat(parsed.matches()).isTrue();199 assertThat(parsed.parameters()).hasSize(3);200 assertThat(parsed.parameters().keySet()).containsExactly("param1", "param2", "param3");201 assertThat(parsed.parameters().values()).containsExactly("v1", "v2", "v3");202 }203 @Test204 public void testDuplicateParameters() {205 assertThatThrownBy(() -> new UrlTemplate("/abc/{param1}{?/def/param1}{?/ghi/param3}"))206 .isInstanceOf(IllegalStateException.class)207 .hasMessage("Multiple parameters are defined with the same name (param1).");208 }209 @Test210 public void testSetSingleParameter() {211 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def{?/param2}/ghi{?/param3}");212 urlParametersTemplate.put("param1", "test1");213 assertThat(urlParametersTemplate.render()).isEqualTo("/abc/test1/def/ghi");214 }215 @Test216 public void testThrowsExceptionForInvalidParameterName() {217 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def{?/param2}/ghi{?/param3}");218 assertThatIllegalArgumentException().isThrownBy(() -> urlParametersTemplate.put("param4", "test4"))219 .withMessage("Invalid parameter name: param4");220 }221 @Test222 public void testSetParametersFromMap() {223 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def{?/param2}/ghi{?/param3}");224 Map<String, String> parameters = new HashMap<>();225 parameters.put("param1", "test1");226 parameters.put("param3", "test3");227 urlParametersTemplate.put(parameters);228 assertThat(urlParametersTemplate.render()).isEqualTo("/abc/test1/def/ghi/test3");229 }230 @Test231 public void testAddParametersFromList() {232 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def{?/param2}/ghi{?/param3}");233 List<String> parameters = Lists.newArrayList("test1", "test2");234 urlParametersTemplate.addAll(parameters);235 assertThat(urlParametersTemplate.render()).isEqualTo("/abc/test1/def/test2/ghi");236 }237 @Test238 public void testAddParametersFromListContainingNull() {239 UrlTemplate urlParametersTemplate = new UrlTemplate("/abc/{param1}/def{?/param2}/ghi{?/param3}");240 List<String> parameters = Arrays.asList("test1", null, "test3");241 urlParametersTemplate.addAll(parameters);242 assertThat(urlParametersTemplate.render()).isEqualTo("/abc/test1/def/ghi/test3");243 }244 private List<String> getParameterNames(UrlTemplate urlTemplate) {245 return urlTemplate.getParameters().stream().map(UrlParameter::getName).collect(toList());246 }247 private List<Boolean> getParameterOptionals(UrlTemplate urlTemplate) {248 return urlTemplate.getParameters().stream().map(UrlParameter::isOptional).collect(toList());249 }250}...
getParameterNames
Using AI Code Generation
1public void getParameterNames() {2 UrlTemplate template = new UrlTemplate("/{lang}/page/{page}/");3 List<String> parameterNames = template.getParameterNames();4 assertEquals(2, parameterNames.size());5 assertEquals("lang", parameterNames.get(0));6 assertEquals("page", parameterNames.get(1));7}8public void getParameterNames() {9 UrlTemplate template = new UrlTemplate("/{lang}/page/{page}/");10 List<String> parameterNames = template.getParameterNames();11 assertEquals(2, parameterNames.size());12 assertEquals("lang", parameterNames.get(0));13 assertEquals("page", parameterNames.get(1));14}15Example 2: Using getParameterValue() method16public void getParameterValue() {17 UrlTemplate template = new UrlTemplate("/{lang}/page/{page}/");18 String parameterValue = template.getParameterValue("lang", "/en/page/1/");19 assertEquals("en", parameterValue);20}21public void getParameterValue() {22 UrlTemplate template = new UrlTemplate("/{lang}/page/{page}/");23 String parameterValue = template.getParameterValue("lang", "/en/page/1/");24 assertEquals("en", parameterValue);25}26Example 3: Using getParameterValues() method
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!