selenium2java writes a script to set second-rate

When I use selenium for testing, sometimes I need to go to the background management interface to set some test data, so I wrote some scripts, which is representative because the setup buttons and course details are not on one page, but on the list page.So it's more troublesome to find.Share the code for your reference.

Let's start with the background page and xml information:

Here is my code:

//Set second price
	public void setSeckillPriceByCourseId(int courseId, int price) throws InterruptedException {
		clickCourse();//Click to enter the course list
		clickFormalCourse();//Select Class List
		int status = findCourseByIdAndSetSeckillPrice(courseId, price);//Find classes according to courseid and set second price
		if (status != 2) {
			outpu(status, "Failed to set second price!");

Here's how:

	 * Page through the course to find the corresponding class
	 * Return Value 1: Find the course, 2: Set the second-click price to complete, 3: The second-click has started, and the course was not found on the first four pages
	public int findCourseByIdAndSetSeckillPrice(int courseId, int price) throws InterruptedException {
		int times = 0;//Markup Page
		int mark = 0;//Tag Status
		while(true) {
			List<WebElement> seckills = findElementsByPartialText("Secondkill");//Get a collection of all secondkill status buttons
			for (int i = 0; i < seckills.size(); i++) {//Traverse through all elements in a collection
				//Get date_course_id to determine if it is equal to the parameter id, then click
				String course = seckills.get(i).getAttribute("data-course_id");//Get the corresponding course id for this element
				if (course.equals(courseId+"")) {//Compare IDS
					mark = 1;//Change flag to indicate course discovery
					String status = seckills.get(i).getText();//Get secondkill status
//					output(status);
					if (status.equals("Start second kill")) {
						scrollToElement(seckills.get(i));//Scroll to the element
						clickByJs(seckills.get(i));//Click through js
						findElementByIdAndClearSendkeys("input-modal_promotion_price", price);//Set second price
						boolean key = true;
						while(key){//Wait for save to complete, confirm again
							if (exists(By.xpath("/html/body/div[7]/h2")) && getTextByXpath("/html/body/div[7]/h2").equals("Operation Successful")) {
								key = false;
						output("Set second price successfully!");
						mark = 2;//Change the marker to indicate that the second kill has been completed
						} else if (status.equals("End secondkill")) {
							output("The course has started killing in seconds!");
							mark = 3;//Mark, indicating killing in seconds
							output("The second kill has started!");
				}//End of Traverse
			if (mark != 0) {
				return mark;
			if (times > 2) {
				output("The course was not found!");
				mark = 4;//Change the tag to indicate that it was not found
				return mark;

The method by which the middle js scrolls to an element:

	//Scroll to an element
	public void scrollToElement(WebElement element) {
		JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].scrollIntoView(true);", element);

The middle js clicks on an element:

	//Click through js
	public void clickByJs(WebElement element) {
		JavascriptExecutor js = (JavascriptExecutor) driver;
		js.executeScript("arguments[0].click();", element);
		//Second click method
//		((JavascriptExecutor) driver).executeScript("arguments[0].click()", question);

Selected Technical Articles

  1. java line of code to print heart
  2. Chinese Language Version of Linux Performance Monitoring Software netdata
  3. Interface Test Code Coverage (jacoco) Schema Sharing
  4. Performance Test Framework
  5. How to have a pleasant performance test on the Linux command line interface
  6. Graphic HTTP Brain Map
  7. How to test probabilistic business interfaces
  8. httpclient handles multiuser simultaneous online
  9. Automatically turn swagger documents into test code
  10. Five lines of code to build a static blog
  11. How httpclient handles 302 redirects
  12. Preliminary Study on Test Framework of Linear Interface Based on java

Selected non-technical articles

  1. Why choose software testing as your career path?
  2. Ten steps to becoming a great Java developer
  3. Programming thinking written to everyone
  4. Barriers to automated testing

Public Number Map ☢️ Come Together~FunTester

Tags: Programming Java Linux Selenium xml

Posted on Thu, 05 Sep 2019 20:06:51 -0700 by programguru