Introduction to the custom framework of PC automation test framework

The above several articles explain the encapsulation of PC automation framework, and the following will explain it.

Let's talk about some positioning methods


extend

  • By.tagName()

You can find elements by their label names, such as

,, these commonly used labels. The difference between this method and the previous two methods is that this method usually searches for more than one element, so it is generally recommended to use findElements method in combination. For example, if we want to find how many buttons are on the page, we can use the tagName button to find them

  • By.cssSelector()

1. According to tagName
  driver.findElement(By.cssSelector("input")
2. according to ID
driver.findElement(By.cssSelector("input ා username")); html tag and ා id
Driver. Findelement (by. Cssselector ("" username ""); just id ""
3. According to className
Single class: driver. Findelement (by. Cssselector (". Username");. Class
Composite class: driver. Findelement (by. Cssselector (". Username.. *");. ClassA. ClassB
4. According to element attribute
1) accurate matching:
[a] driver.findelement (by.cssselector ("input[name=username])); attribute name = attribute value, id,class, etc. can be written in this form
[b] driver.findelement (by.cssselector ("img[alt])); property exists. For example, the IMG element has an alt attribute
[C] driver.findelement (by.cssselector ("input[type ='submit '] [value ='Login'])); multi attribute
2) fuzzy matching: (regular expression matching attribute)
[a] driver.findelement (by.cssselector (input [id ^ = 'Ctrl')); matching to id header such as ctrl_12
[b] driver.findelement (by.cssselector (input [id $= 'ctrl')); match to the tail of id, such as a \
[C] driver.findelement (by.cssselector (input [id * = 'ctrl')); match to the middle of id, such as 1 ﹣ ctrl ﹣ 1

Here comes the actual battle!!! The use of various methods, as well as the use of positioning methods (here take a tourism website as an example)

	public static void main(String[] args) throws Exception {
		/*
		// 1: driver: Initialize driver
		UserFunction.driver("https://XXXXXXXXXX");
		
		// 2: activate: Click element
		UserFunction.activate(By.className("over"));
		
		// 3: activateByIndex: Click the element with the same attribute and different location
		UserFunction.activateByIndex(By.className("over"), 1);
		UserFunction.activate(By.className("logo"));
		
		// 4: enterTextIn: Enter text information
		UserFunction.enterTextIn(By.id("pt__search_text"), "1045201");
		
		//  5:  enterTextInByText: Input information of elements with the same attribute and different location
		UserFunction.driver("http://passport.ly.com");
		UserFunction.activate(By.id("switchPcLogin"));
		UserFunction.enterTextInByIndex(By.tagName("input"), 4,"123");
		
		// 6: selectFromList: Select the element of the drop-down box
		UserFunction.selectFromList(By.name("adultNum"), "3");
		
		// 7: getTextFrom: Get the text information of the element
		String text = UserFunction.getTextFrom(By.className("J_priceS"));
		System.out.println(text);
		
		// 8: getTitle: Get the title of the page
		String title = UserFunction.getTitle();
		System.out.println(title);
		
		// 9: getAttribute: Get another attribute value according to the element property
		String value = UserFunction.getAttribute(By.className("logo"), "title");
		System.out.println(value);
		
		// 10: getCurrentUrl: Get the URL value of the current page
		String url = UserFunction.getCurrentUrl();
		System.out.println(url);
		
		// 14: key_tab: Simulate keyboard Tab operation
		UserFunction.activate(By.className("over"));
		BasicFunction.sleep(3000);// Because the immediate booking button already exists, but if the resource is not loaded, clicking the immediate booking button will not jump to the order page.
		UserFunction.activate(By.cssSelector("span.choose.J-Next"));// Immediate Booking
		UserFunction.activate(By.id("Iname"));
		UserFunction.key_tab(5);
		
		// 15: key_button: Analog keyboard up and down key operation
		UserFunction.key_button(5, "down");
		UserFunction.key_button(3, "up");
		
		// 16: switchWindow: switch windows
		UserFunction.driver("http://www.ly.com");
		UserFunction.activate(By.className("logo"));
		UserFunction.enterTextIn(By.id("pt__search_text"), "1045201");
		UserFunction.switchWindow(1);
		UserFunction.enterTextIn(By.id("pt__search_text"), "1045201");
		
		// 17:isElementExist: Judge whether the current page is
		if (UserFunction.isElementExist(By.className("order-btn"))){
			System.out.println("The book now button has been loaded ');
		}else{
			System.out.println("Element does not exist ');
		}
		
		// 18: clear: Clear input box content
		UserFunction.activate(By.className("logo"));
		UserFunction.switchWindow(1);
		UserFunction.enterTextIn(By.id("pt__search_text"), "1045201");
		UserFunction.clear(By.id("pt__search_text"));
		
		// 19: close: Close the current window
		UserFunction.activate(By.className("logo"));
		UserFunction.close();
		
		// 20: quit: Close browser
		UserFunction.activate(By.className("logo"));
		UserFunction.quit();
		
		// 21: refresh: Refresh page
		UserFunction.refresh();
		
		// 22: screenShot: screenshot
		UserFunction.screenShot(); //Operation to determine whether a folder exists
		
		// 23: screenShotSpecial: Abnormal screenshots
		UserFunction.screenShotSpecial();

		// Take the landing page for example, several positioning methods
		UserFunction.driver("http://passport.ly.com");
		//By.id() Find by id attribute
		dr.findElement(By.id("switchPcLogin")).click();
		// By.name() Find by name attribute
		dr.findElement(By.id("switchPcLogin")).click();
		dr.findElement(By.name("name")).sendKeys("18550502316");
		// By.className() Find by class attribute
		dr.findElement(By.className("switch_corner_pc")).click();
		// By.linkText() Through linkText (link text exact search)
		dr.findElement(By.linkText("Quick registration '). click();
		// By.partiallinktext()  Link text fuzzy search 
		dr.findElement(By.partialLinkText("Login page "). click();
		// By.xpath() Via xpath path
		dr.findElement(By.xpath("//*[@id='switchPcLogin']")).click();
		//UserFunction.activate(By.xpath("//*[@id='switchPcLogin']"));  //copy Pay attention to the single and double quotation marks.
		// By.cssSelector()	Several positioning methods of selector
		// By.tagName() Find by tag
		List<WebElement> list=dr.findElements(By.cssSelector("input"));
		System.out.println(list.size());
		// 2:According to ID
		dr.findElement(By.cssSelector("#switchPcLogin")).click();
		// 3:According to className
		dr.findElement(By.cssSelector(".switch_corner")).click();
		dr.findElement(By.cssSelector("")).click(); //Composite immediate booking button
		*/
	}

Tags: Attribute Windows

Posted on Sun, 10 Nov 2019 13:54:37 -0800 by adrianpeyton