Best Testng code snippet using org.testng.TestRunner.getAttribute
Source:SuiteRunner.java
...524 }525 }526 private IAttributes m_attributes = new Attributes();527 @Override528 public Object getAttribute(String name) {529 return m_attributes.getAttribute(name);530 }531 @Override532 public void setAttribute(String name, Object value) {533 m_attributes.setAttribute(name, value);534 }535 @Override536 public Set<String> getAttributeNames() {537 return m_attributes.getAttributeNames();538 }539 @Override540 public Object removeAttribute(String name) {541 return m_attributes.removeAttribute(name);542 }543 /////544 // implements IInvokedMethodListener545 //546 @Override547 public void afterInvocation(IInvokedMethod method, ITestResult testResult) {548 }549 @Override550 public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {551 if (method == null) {...
Source:NginxRebindIntegrationTest.java
...118 119 // Start the app, and ensure reachable; start polling the URL120 origApp.start(ImmutableList.of(localhostProvisioningLocation));121 122 String rootUrl = origNginx.getAttribute(NginxController.ROOT_URL);123 int nginxPort = origNginx.getAttribute(NginxController.PROXY_HTTP_PORT);124 125 assertHttpStatusCodeEventuallyEquals(rootUrl, 404);126 WebAppMonitor monitor = newWebAppMonitor(rootUrl, 404);127 final String origConfigFile = origNginx.getConfigFile();128 129 newApp = rebind(RebindOptions.create().terminateOrigManagementContext(true));130 final NginxController newNginx = (NginxController) Iterables.find(newApp.getChildren(), Predicates.instanceOf(NginxController.class));131 assertEquals(newNginx.getConfigFile(), origConfigFile);132 133 EntityTestUtils.assertAttributeEqualsEventually(newNginx, NginxController.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING);134 assertEquals(newNginx.getAttribute(NginxController.PROXY_HTTP_PORT), (Integer)nginxPort);135 assertEquals(newNginx.getAttribute(NginxController.ROOT_URL), rootUrl);136 assertEquals(newNginx.getAttribute(NginxController.PROXY_HTTP_PORT), origNginx.getAttribute(NginxController.PROXY_HTTP_PORT));137 assertEquals(newNginx.getConfig(NginxController.STICKY), origNginx.getConfig(NginxController.STICKY));138 139 assertAttributeEqualsEventually(newNginx, SoftwareProcess.SERVICE_UP, true);140 assertHttpStatusCodeEventuallyEquals(rootUrl, 404);141 142 assertEquals(monitor.getFailures(), 0);143 }144 145 /*146 Exception java.lang.NoClassDefFoundError147 148 Message: org/apache/brooklyn/test/HttpTestUtils$3149 Stacktrace:150 151 152 at org.apache.brooklyn.test.HttpTestUtils.assertHttpStatusCodeEventuallyEquals(HttpTestUtils.java:208)153 at org.apache.brooklyn.test.HttpTestUtils.assertHttpStatusCodeEventuallyEquals(HttpTestUtils.java:204)154 at org.apache.brooklyn.entity.proxy.nginx.NginxRebindIntegrationTest.testRebindsWithoutLosingServerPool(NginxRebindIntegrationTest.java:178)155 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)156 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)157 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)158 at java.lang.reflect.Method.invoke(Method.java:606)159 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)160 at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)161 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)162 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)163 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)164 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)165 at org.testng.TestRunner.privateRun(TestRunner.java:767)166 at org.testng.TestRunner.run(TestRunner.java:617)167 at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)168 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)169 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)170 at org.testng.SuiteRunner.run(SuiteRunner.java:254)171 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)172 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)173 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)174 at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)175 at org.testng.TestNG.run(TestNG.java:1057)176 at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:115)177 at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:205)178 at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:108)179 at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:111)180 at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)181 at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)182 at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)183 */184 /**185 * Test can rebind with an active server pool.186 */187 @Test(groups = {"Integration","Broken"})188 public void testRebindsWithoutLosingServerPool() throws Exception {189 190 // Set up nginx with a server pool191 DynamicCluster origServerPool = origApp.createAndManageChild(EntitySpec.create(DynamicCluster.class)192 .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(Tomcat8Server.class).configure("war", getTestWar()))193 .configure("initialSize", 1));194 195 NginxController origNginx = origApp.createAndManageChild(EntitySpec.create(NginxController.class)196 .configure("serverPool", origServerPool)197 .configure("domain", "localhost"));198 199 // Start the app, and ensure reachable; start polling the URL200 origApp.start(ImmutableList.of(localhostProvisioningLocation));201 202 String rootUrl = origNginx.getAttribute(NginxController.ROOT_URL);203 Tomcat8Server origServer = (Tomcat8Server) Iterables.getOnlyElement(origServerPool.getMembers());204 assertEquals(origNginx.getAttribute(NginxController.SERVER_POOL_TARGETS).keySet(), ImmutableSet.of(origServer));205 206 assertHttpStatusCodeEventuallyEquals(rootUrl, 200);207 WebAppMonitor monitor = newWebAppMonitor(rootUrl, 200);208 final String origConfigFile = origNginx.getConfigFile();209 210 // Rebind211 newApp = rebind(RebindOptions.create().terminateOrigManagementContext(true));212 ManagementContext newManagementContext = newApp.getManagementContext();213 final NginxController newNginx = (NginxController) Iterables.find(newApp.getChildren(), Predicates.instanceOf(NginxController.class));214 final DynamicCluster newServerPool = (DynamicCluster) newManagementContext.getEntityManager().getEntity(origServerPool.getId());215 final Tomcat8Server newServer = (Tomcat8Server) Iterables.getOnlyElement(newServerPool.getMembers());216 // Expect continually to have same nginx members; should not lose them temporarily!217 Asserts.succeedsContinually(new Runnable() {218 public void run() {219 Map<Entity, String> newNginxMemebers = newNginx.getAttribute(NginxController.SERVER_POOL_TARGETS);220 assertEquals(newNginxMemebers.keySet(), ImmutableSet.of(newServer));221 }});222 223 224 assertAttributeEqualsEventually(newNginx, SoftwareProcess.SERVICE_UP, true);225 assertHttpStatusCodeEventuallyEquals(rootUrl, 200);226 assertEquals(newNginx.getConfigFile(), origConfigFile);227 228 // Check that an update doesn't break things229 newNginx.update();230 assertHttpStatusCodeEquals(rootUrl, 200);231 // Resize new cluster, and confirm change takes affect.232 // - Increase size233 // - wait for nginx to definitely be updates (TODO nicer way to wait for updated?)234 // - terminate old server235 // - confirm can still route messages236 newServerPool.resize(2);237 238 Thread.sleep(10*1000);239 240 newServer.stop();241 assertHttpStatusCodeEventuallyEquals(rootUrl, 200);242 // Check that URLs have been constantly reachable243 assertEquals(monitor.getFailures(), 0);244 }245 246 /*247 Exception java.lang.NoClassDefFoundError248 249 Message: org/apache/brooklyn/test/HttpTestUtils$3250 Stacktrace:251 252 253 at org.apache.brooklyn.test.HttpTestUtils.assertHttpStatusCodeEventuallyEquals(HttpTestUtils.java:208)254 at org.apache.brooklyn.test.HttpTestUtils.assertHttpStatusCodeEventuallyEquals(HttpTestUtils.java:204)255 at org.apache.brooklyn.entity.proxy.nginx.NginxRebindIntegrationTest.testRebindsWithoutLosingUrlMappings(NginxRebindIntegrationTest.java:254)256 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)257 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)258 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)259 at java.lang.reflect.Method.invoke(Method.java:606)260 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)261 at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)262 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)263 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)264 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)265 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)266 at org.testng.TestRunner.privateRun(TestRunner.java:767)267 at org.testng.TestRunner.run(TestRunner.java:617)268 at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)269 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)270 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)271 at org.testng.SuiteRunner.run(SuiteRunner.java:254)272 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)273 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)274 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)275 at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)276 at org.testng.TestNG.run(TestNG.java:1057)277 at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:115)278 at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:205)279 at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:108)280 at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:111)281 at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)282 at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)283 at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)284 */285 /**286 * Test can rebind to the with server pool and URL remappings.287 * NOTE: This requires a redirection from localhost1 to 127.0.0.1 in your /etc/hosts file288 */289 @Test(groups = {"Integration","Broken"})290 public void testRebindsWithoutLosingUrlMappings() throws Exception {291 292 // Set up nginx with a url-mapping293 Group origUrlMappingsGroup = origApp.createAndManageChild(EntitySpec.create(BasicGroup.class)294 .configure("childrenAsMembers", true));295 296 DynamicCluster origServerPool = origApp.createAndManageChild(EntitySpec.create(DynamicCluster.class)297 .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(Tomcat8Server.class).configure("war", getTestWar()))298 .configure("initialSize", 1)); 299 UrlMapping origMapping = origUrlMappingsGroup.addChild(EntitySpec.create(UrlMapping.class)300 .configure("domain", "localhost1")301 .configure("target", origServerPool)302 .configure("rewrites", ImmutableList.of(new UrlRewriteRule("/foo/(.*)", "/$1"))));303 304 NginxController origNginx = origApp.createAndManageChild(EntitySpec.create(NginxController.class)305 .configure("domain", "localhost")306 .configure("urlMappings", origUrlMappingsGroup));307 // Start the app, and ensure reachable; start polling the URL308 origApp.start(ImmutableList.of(localhostProvisioningLocation));309 310 String mappingGroupUrl = "http://localhost1:"+origNginx.getAttribute(NginxController.PROXY_HTTP_PORT)+"/foo/";311 assertHttpStatusCodeEventuallyEquals(mappingGroupUrl, 200);312 WebAppMonitor monitor = newWebAppMonitor(mappingGroupUrl, 200);313 final String origConfigFile = origNginx.getConfigFile();314 315 // Create a rebinding316 newApp = rebind(RebindOptions.create().terminateOrigManagementContext(true));317 ManagementContext newManagementContext = newApp.getManagementContext();318 final NginxController newNginx = (NginxController) Iterables.find(newApp.getChildren(), Predicates.instanceOf(NginxController.class));319 DynamicCluster newServerPool = (DynamicCluster) newManagementContext.getEntityManager().getEntity(origServerPool.getId());320 Tomcat8Server newServer = (Tomcat8Server) Iterables.getOnlyElement(newServerPool.getMembers());321 322 assertAttributeEqualsEventually(newNginx, SoftwareProcess.SERVICE_UP, true);323 assertHttpStatusCodeEventuallyEquals(mappingGroupUrl, 200);324 ...
Source:DynamicWebAppClusterRebindIntegrationTest.java
...144 .configure("initialSize", 1));145 146 origApp.start(ImmutableList.of(localhostProvisioningLocation));147 JBoss7Server origJboss = (JBoss7Server) Iterables.find(origCluster.getChildren(), Predicates.instanceOf(JBoss7Server.class));148 String jbossUrl = origJboss.getAttribute(JBoss7Server.ROOT_URL);149 150 assertHttpStatusCodeEventuallyEquals(jbossUrl, 200);151 WebAppMonitor monitor = newWebAppMonitor(jbossUrl);152 153 // Rebind154 newApp = rebind();155 DynamicWebAppCluster newCluster = (DynamicWebAppCluster) Iterables.find(newApp.getChildren(), Predicates.instanceOf(DynamicWebAppCluster.class));156 assertHttpStatusCodeEquals(jbossUrl, 200);157 // Confirm the cluster is usable: we can scale-up158 assertEquals(newCluster.getCurrentSize(), (Integer)1);159 newCluster.resize(2);160 Iterable<Entity> newJbosses = Iterables.filter(newCluster.getChildren(), Predicates.instanceOf(JBoss7Server.class));161 assertEquals(Iterables.size(newJbosses), 2);162 for (Entity j : newJbosses) {163 assertHttpStatusCodeEventuallyEquals(j.getAttribute(JBoss7Server.ROOT_URL), 200);164 }165 // Ensure while doing all of this the original jboss server remained reachable166 assertEquals(monitor.getFailures(), 0);167 168 // Ensure cluster is usable: we can scale back to stop the original jboss server169 newCluster.resize(0);170 171 assertUrlUnreachableEventually(jbossUrl);172 }173}...
Source:DropdownSession4.java
...38 WebElement birthdate = driver.findElement(By.name("birthday_day"));39 Thread.sleep(3000);40 Select sel1 = new Select(birthdate);41 sel1.selectByIndex(1);42 String birthdateverify = driver.findElement(By.name("birthday_day")).getAttribute("value");43 Assert.assertEquals(birthdateverify, "1", "If values does not match it should fail");44 }45 @Test (priority =2)46 public void Verifybirthmonth(){47 WebElement birthmonth = driver.findElement(By.name("birthday_month"));48 Select sel2 = new Select(birthmonth);49 sel2.selectByValue("3");50 //String birthmontheverify = driver.findElement(By.name("birthday_month")).getText();51 //Assert.assertTrue(birthmontheverify.contains("Mar"), "If birthmonthverify does not match it should fail");52 53 WebElement selected_value=sel2.getFirstSelectedOption();54 Assert.assertEquals(selected_value.getText(), "Mar");55 56 57 }58 @Test (priority =3)59 public void Verifybirthyear(){60 WebElement birthyear = (WebElement) driver.findElement(By.name("birthday_year"));61 Select sel3 = new Select(birthyear);62 List<WebElement> birthyearelement = sel3.getOptions();63 64 int monthlist = birthyearelement.size();65 System.out.println(monthlist);66 67 for(WebElement ele: birthyearelement){68 String Allvalues = ele.getText();69 System.out.println("values are :" + Allvalues);70 71 }72 //sel3.selectByVisibleText("1991"); 73 //String birthyearverify = driver.findElement(By.name("birthday_year")).getAttribute("value");74 //Assert.assertEquals(birthyearverify, "1991", "If values does not match it should fail");75 driver.close();76 } 77 78 79 }80
...
getAttribute
Using AI Code Generation
1public void test(){2 TestRunner testRunner = new TestRunner();3 testRunner.setAttribute("key1", "value1");4 testRunner.setAttribute("key2", "value2");5 testRunner.setAttribute("key3", "value3");6 testRunner.setAttribute("key4", "value4");7 testRunner.setAttribute("key5", "value5");8 testRunner.setAttribute("key6", "value6");9 testRunner.setAttribute("key7", "value7");10 testRunner.setAttribute("key8", "value8");11 testRunner.setAttribute("key9", "value9");12 testRunner.setAttribute("key10", "value10");13 testRunner.setAttribute("key11", "value11");14 testRunner.setAttribute("key12", "value12");15 testRunner.setAttribute("key13", "value13");16 testRunner.setAttribute("key14", "value14");17 testRunner.setAttribute("key15", "value15");18 testRunner.setAttribute("key16", "value16");19 testRunner.setAttribute("key17", "value17");20 testRunner.setAttribute("key18", "value18");21 testRunner.setAttribute("key19", "value19");22 testRunner.setAttribute("key20", "value20");23 testRunner.setAttribute("key21", "value21");24 testRunner.setAttribute("key22", "value22");25 testRunner.setAttribute("key23", "value23");26 testRunner.setAttribute("key24", "value24");27 testRunner.setAttribute("key25", "value25");28 testRunner.setAttribute("key26", "value26");29 testRunner.setAttribute("key27", "value27");30 testRunner.setAttribute("key28", "value28");31 testRunner.setAttribute("key29", "value29");32 testRunner.setAttribute("key30", "value30");33 testRunner.setAttribute("key31", "value31");34 testRunner.setAttribute("key32", "value32");35 testRunner.setAttribute("key33", "value33");36 testRunner.setAttribute("key34", "value34");37 testRunner.setAttribute("key35", "value35");38 testRunner.setAttribute("key36", "value36");39 testRunner.setAttribute("key37", "value37");40 testRunner.setAttribute("key38", "value38");41 testRunner.setAttribute("key39", "value39");42 testRunner.setAttribute("key40", "value40");
getAttribute
Using AI Code Generation
1import org.testng.TestRunner2import org.testng.ITestResult3import org.testng.ITestContext4def testName = testContext.getAttribute("testName")5println "testName = ${testName}"6import org.testng.ITestContext7def testName = testContext.getAttribute("testName")8println "testName = ${testName}"9import org.testng.ITestResult10def testName = testResult.getAttribute("testName")11println "testName = ${testName}"12import org.testng.ITestResult13def testName = testContext.getAttribute("testName")14println "testName = ${testName}"15import org.testng.ITestResult16def testName = testContext.getAttribute("testName")17println "testName = ${testName}"18import org.testng.ITestResult19def testName = testContext.getAttribute("testName")20println "testName = ${testName}"21import org.testng.ITestResult22def testName = testContext.getAttribute("testName")23println "testName = ${testName}"24import org.testng.ITestResult
getAttribute
Using AI Code Generation
1import org.testng.TestNG;2import org.testng.TestRunner;3public class GetAttributeDemo {4 public static void main(String[] args) {5 TestNG testNG = new TestNG();6 testNG.setTestClasses(new Class[] {MyTest.class});7 testNG.run();8 TestRunner testRunner = testNG.getTestRunner();9 testRunner.setAttribute("myAttribute", "myValue");10 System.out.println(testRunner.getAttribute("myAttribute"));11 }12}
getAttribute
Using AI Code Generation
1String attributeValue = testRunner.getAttribute("attributeName");2if(attributeValue.equals("yes"))3{4}5if(attributeValue.equals("no"))6{7}8if(attributeValue.equals("yes"))9{10}11else if(attributeValue.equals("no"))12{13}14if(attributeValue.equals("yes"))15{16}17else if(attributeValue.equals("no"))18{19}20else if(attributeValue.equals("maybe"))21{22}23else if(attributeValue.equals("perhaps"))24{25}26else if(attributeValue.equals("perhaps"))27{28}
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!