Advertisement
Guest User

Untitled

a guest
Jul 13th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.43 KB | None | 0 0
  1. //
  2. //  LoveTweetUITests.swift
  3. //  LoveTweetUITests
  4. //
  5. //  Created by Daniel Fera on 13/07/2018.
  6. //  Copyright Β© 2018 YiGu. All rights reserved.
  7. //
  8.  
  9. import XCTest
  10.  
  11. extension XCUIElement {
  12.     /**
  13.      Removes any current text in the field before typing in the new value
  14.      - Parameter text: the text to enter into the field
  15.      */
  16.     func clearAndEnterText(text: String) {
  17.         guard let stringValue = self.value as? String else {
  18.             XCTFail("Tried to clear and enter text into a non string value")
  19.             return
  20.         }
  21.        
  22.         self.tap()
  23.        
  24.         let deleteString = String(repeating: XCUIKeyboardKey.delete.rawValue, count: stringValue.count)
  25.        
  26.         self.typeText(deleteString)
  27.         self.typeText(text)
  28.         self.typeText("\n")
  29.     }
  30. }
  31.  
  32. class LoveTweetUITests: XCTestCase {
  33.        
  34.     override func setUp() {
  35.         super.setUp()
  36.        
  37.         // Put setup code here. This method is called before the invocation of each test method in the class.
  38.        
  39.         // In UI tests it is usually best to stop immediately when a failure occurs.
  40.         continueAfterFailure = false
  41.         // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
  42.         XCUIApplication().launch()
  43.  
  44.         // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
  45.     }
  46.    
  47.     override func tearDown() {
  48.         // Put teardown code here. This method is called after the invocation of each test method in the class.
  49.         super.tearDown()
  50.     }
  51.    
  52.     func getStringSalary(salary: Float) -> String {
  53.         let ksal = Int(salary)
  54.         return "$\(ksal)k"
  55.     }
  56.    
  57.     func testThatNameTextFieldExists() {
  58.  
  59.         let app = XCUIApplication()
  60.         let nameTextField = app.scrollViews.otherElements.containing(.staticText, identifier:"LoveTweet").children(matching: .other).element(boundBy: 0).children(matching: .textField).element
  61.         nameTextField.tap()
  62.         nameTextField.typeText("TestNume")
  63.         sleep(1)
  64.         XCTAssertEqual(nameTextField.exists, true)
  65.        
  66.     }
  67.    
  68.     func testThatTweetItButtonsWorksWithAllInfos() {
  69.         let name = "TestNume"
  70.         let birthDate = ("1996", "November", "21")
  71.         let salary = 140.0
  72.         let maxSalary = 350.0
  73.         let normalizedSalary0 = (140.0/maxSalary)
  74.         let normalizedSalary = CGFloat(normalizedSalary0)
  75.         let work = "TestWork"
  76.         let interestedGender = "Women"
  77.         let dateFormatter = DateFormatter()
  78.        
  79.         dateFormatter.dateFormat = "yyyy' - 'MMMM' - 'dd"
  80.         let dateBirth = dateFormatter.date(from: "\(birthDate.0) - \(birthDate.1) - \(birthDate.2)")
  81.         let minAge = Date().timeIntervalSince(dateBirth!)
  82.         let age = Int(minAge / (3600 * 24 * 365))
  83.        
  84.         let app = XCUIApplication()
  85.         let nameTextField = app.textFields.firstMatch
  86.         nameTextField.tap()
  87.         nameTextField.clearAndEnterText(text: name)
  88.        
  89.         let femaleButton = app.scrollViews.otherElements.buttons["Female"]
  90.         let maleButton = app.scrollViews.otherElements.buttons["Male"]
  91.        
  92.        
  93.         let workTextField = app.textFields.element(boundBy: 1)
  94.         workTextField.tap()
  95.         workTextField.clearAndEnterText(text: work)
  96.         femaleButton.tap()
  97.         maleButton.tap()
  98.    
  99.         let datePicker = app.scrollViews.otherElements.datePickers
  100.        
  101.         datePicker.pickerWheels["1995"].adjust(toPickerWheelValue: birthDate.0)
  102.         datePicker.pickerWheels["February"].adjust(toPickerWheelValue: birthDate.1)
  103.         datePicker.pickerWheels["24"].adjust(toPickerWheelValue: birthDate.2)
  104.         let salarySlider = app.sliders.firstMatch
  105.         salarySlider.adjust(toNormalizedSliderPosition: normalizedSalary)
  106.        
  107.         let tweetItButton = app.scrollViews.otherElements.buttons["Tweet it!"]
  108.         tweetItButton.tap()
  109.        
  110.         let loveTweetAlert = app.alerts["Love Tweet"]
  111.         let successMessage = "Hi, I am \(name). As a \(age)-year-old \(work) earning \(getStringSalary(salary: Float(salary)))/year, I am interested in \(interestedGender). Feel free to contact me!"
  112.         print(successMessage)
  113.        
  114.         XCTAssertEqual(loveTweetAlert.staticTexts[successMessage].exists, true)
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement