Getting Started with Selenium Web Driver in Java

Getting Started with Selenium Web Driver in Java

This article is written upon the request of people in the community. How we can kick start Selenium WebDriver in Java.

To start with Selenium WebDriver in Java first we need to install JDK 1.8 which is most used and stable JDK version.

Next we need to add an IDE. I love eclipse. So lets download and install eclipse IDE

There after we need to add a project and lets open up and add a maven project.

Go to File > New > Maven Project

Click on creating simple project

No alt text provided for this image

Next give a meaning full artifact ID and Group ID

No alt text provided for this image

After clicking finish you will have your maven project.

Now lets add the selenium, testng and web driver manager dependencies to pom.xml

 <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
  </dependency>
  <dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>3.4.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</version>
    <scope>test</scope>
</dependency>

Once done save the pom.xml and all the dependencies will be downloaded.

Now add the package and a class, and add the following script.

		@Test
		
		public void test() 
		{
		
			 
			WebDriverManager.chromedriver().setup();
			
			WebDriver driver = new ChromeDriver();
	    	
	        String baseUrl = "https://demo.guru99.com/V4/";
	      


	        driver.get(baseUrl);
	        
	        driver.findElement(By.name("uid")).sendKeys("mngr206361");
	        driver.findElement(By.name("password")).sendKeys("AdUbupU");
	   
	        driver.findElement(By.name("btnLogin")).click();


	       // System.out.println (driver.findElement(By.xpath("https://tr[@class='heading3']/td")).getText());
	      Assert.assertEquals(driver.findElement(By.xpath("https://tr[@class='heading3']/td")).getText(), "Manger Id : mngr206361");
	        driver.close();
	        driver.quit();
		}
	    

Run it as a testng and you will your script will get executed in the chrome browser.

要查看或添加评论,请登录

Kushan Shalindra Amarasiri的更多文章

社区洞察

其他会员也浏览了