input.test.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. describe('component-native-input', () => {
  2. const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
  3. const isAndroid = platformInfo.startsWith('android')
  4. const isIOS = platformInfo.startsWith('ios')
  5. const isMP = platformInfo.startsWith('mp')
  6. const isWeb = platformInfo.startsWith('web')
  7. const isHarmony = platformInfo.startsWith('harmony')
  8. let page;
  9. beforeAll(async () => {
  10. page = await program.reLaunch('/pages/component/input/input')
  11. await page.waitFor('view');
  12. });
  13. // it("beforeAllTestScreenshot", async () => {
  14. // const image = await program.screenshot({
  15. // fullPage: true
  16. // })
  17. // expect(image).toSaveImageSnapshot()
  18. // })
  19. // 测试焦点及键盘弹起
  20. if(!isMP) {
  21. it('focus', async () => {
  22. const input = await page.$('#uni-input-focus');
  23. expect(await input.attribute('focus')).toBe("true")
  24. // expect(await page.data("inputFocusKeyBoardChangeValue")).toBe(true)
  25. await page.setData({
  26. focus: false,
  27. })
  28. expect(await input.attribute('focus')).toBe("false")
  29. // await page.waitFor(1000)
  30. // expect(await page.data("inputFocusKeyBoardChangeValue")).toBe(false)
  31. // await page.setData({
  32. // focus: true,
  33. // })
  34. // expect(await input.attribute('focus')).toBe(true)
  35. // await page.waitFor(1000)
  36. // expect(await page.data("inputFocusKeyBoardChangeValue")).toBe(true)
  37. // await page.setData({
  38. // focus: false,
  39. // })
  40. // expect(await input.attribute('focus')).toBe(false)
  41. // await page.waitFor(1000)
  42. // expect(await page.data("inputFocusKeyBoardChangeValue")).toBe(false)
  43. // await page.waitFor(1000)
  44. });
  45. }
  46. // 测试修改value属性
  47. it("value", async () => {
  48. const input = await page.$('#uni-input-default');
  49. expect(await input.property('value')).toEqual("hello uni-app x")
  50. })
  51. //测试input的类型
  52. it("type", async () => {
  53. const text = await page.$('#uni-input-type-text');
  54. const number = await page.$('#uni-input-type-number');
  55. const digit = await page.$('#uni-input-type-digit');
  56. const tel = await page.$('#uni-input-type-tel');
  57. expect(await text.attribute('type')).toEqual("text")
  58. expect(await number.attribute('type')).toEqual("number")
  59. expect(await digit.attribute('type')).toEqual("digit")
  60. expect(await tel.attribute('type')).toEqual("tel")
  61. })
  62. // 测试密码属性
  63. // it("password", async () => {
  64. // const input = await page.$('.uni-input-password');
  65. // expect(await input.attribute('password')).toBe(true)
  66. // await page.setData({
  67. // inputPassword: false,
  68. // inputPasswordValue: "inputPasswordValue"
  69. // })
  70. // expect(await input.attribute('password')).toBe(false)
  71. // await page.waitFor(500)
  72. // await page.setData({
  73. // inputPassword: true
  74. // })
  75. // })
  76. // 测试placeholder
  77. // it("placeholder", async () => {
  78. // const placeholder1 = await page.$('.uni-input-placeholder1');
  79. // expect(await placeholder1.attribute("placeholder-style")).toMatchObject({
  80. // "color": "red"
  81. // })
  82. // expect(await placeholder1.attribute("placeholder")).toEqual("占位符文字颜色为红色")
  83. // await page.setData({
  84. // inputPlaceHolderStyle: "color:#CC00CC",
  85. // })
  86. // expect(await placeholder1.attribute("placeholder-style")).toMatchObject({
  87. // "color": "#CC00CC"
  88. // })
  89. // await page.setData({
  90. // inputPlaceHolderStyle: "color:#CC19CC;background-color:#00b1c0",
  91. // })
  92. // expect(await placeholder1.attribute("placeholder-style")).toMatchObject({
  93. // "color": "#CC19CC",
  94. // "backgroundColor": "#00b1c0"
  95. // })
  96. // await page.setData({
  97. // inputPlaceHolderStyle: "color:#CC19CC;background-color:#00b1c0;text-align:center;font-size:44px;font-weight:900",
  98. // })
  99. // expect(await placeholder1.attribute("placeholder-style")).toEqual({
  100. // "backgroundColor": "#00b1c0",
  101. // "color": "#CC19CC",
  102. // "fontSize": "44px",
  103. // "fontWeight": "900",
  104. // "textAlign": "center"
  105. // })
  106. // const placeholder2 = await page.$('.uni-input-placeholder2');
  107. // expect(await placeholder2.attribute("placeholder-class")).toMatchObject({
  108. // "backgroundColor": "#008000"
  109. // })
  110. // await page.setData({
  111. // inputPlaceHolderClass: "uni-input-placeholder-class-ts",
  112. // })
  113. // expect(await placeholder2.attribute("placeholder-class")).toMatchObject({
  114. // "backgroundColor": "#FFA500"
  115. // })
  116. // expect(await placeholder2.attribute("placeholder")).toEqual("占位符背景色为绿色")
  117. // })
  118. if(isMP) {
  119. it("disable", async () => {
  120. const input = await page.$('#uni-input-disable');
  121. expect(await input.property("disabled")).toBe(true)
  122. })
  123. // 如下属性在自动化测试通过property、attribute + confirmType、confirm-type均无法获取
  124. // it("confirm-type", async () => {
  125. // expect(await (await page.$('#uni-input-confirm-send')).attribute("confirm-type")).toEqual("send")
  126. // expect(await (await page.$('#uni-input-confirm-search')).property("confirmType")).toEqual("search")
  127. // expect(await (await page.$('#uni-input-confirm-next')).property("confirmType")).toEqual("next")
  128. // expect(await (await page.$('#uni-input-confirm-go')).property("confirmType")).toEqual("go")
  129. // expect(await (await page.$('#uni-input-confirm-done')).property("confirmType")).toEqual("done")
  130. // })
  131. // it("cursor-color", async () => {
  132. // await page.setData({
  133. // cursor_color: "red",
  134. // })
  135. // await page.waitFor(500)
  136. // expect(await (await page.$('#uni-input-cursor-color')).property("cursor-color")).toBe("red")
  137. // })
  138. } else {
  139. it("disable", async () => {
  140. const input = await page.$('#uni-input-disable');
  141. expect(await input.attribute("disabled")).toBe("true")
  142. })
  143. it("confirm-type", async () => {
  144. expect(await (await page.$('#uni-input-confirm-send')).attribute("confirmType")).toEqual("send")
  145. expect(await (await page.$('#uni-input-confirm-search')).attribute("confirmType")).toEqual("search")
  146. expect(await (await page.$('#uni-input-confirm-next')).attribute("confirmType")).toEqual("next")
  147. expect(await (await page.$('#uni-input-confirm-go')).attribute("confirmType")).toEqual("go")
  148. expect(await (await page.$('#uni-input-confirm-done')).attribute("confirmType")).toEqual("done")
  149. })
  150. it("cursor-color", async () => {
  151. await page.setData({
  152. cursor_color: "red",
  153. })
  154. await page.waitFor(500)
  155. expect(await (await page.$('#uni-input-cursor-color')).attribute("cursor-color")).toBe("red")
  156. })
  157. }
  158. // it("maxlength", async () => {
  159. // const input = await page.$('.uni-input-maxlength');
  160. // await page.setData({
  161. // inputMaxLengthValue: "uni-input-maxlength"
  162. // })
  163. // await page.waitFor(500)
  164. // })
  165. it("maxlength", async () => {
  166. const input = await page.$('#uni-input-maxlength');
  167. let str = "";
  168. for (let i = 0; i < 200; i++) {
  169. str += `${i}`
  170. }
  171. await page.setData({
  172. inputMaxLengthValue: str
  173. })
  174. let length = (await input.value()).length
  175. expect(length).toBe(10)
  176. await page.setData({
  177. inputMaxLengthValue: ""
  178. })
  179. })
  180. it("password and value order", async () => {
  181. const input = await page.$('#uni-input-password');
  182. let length = (await input.value()).length
  183. expect(length).toBe(6)
  184. await page.setData({
  185. inputPasswordValue: ""
  186. })
  187. })
  188. it("keyboard height changed after page back", async () => {
  189. if (isWeb || isMP || isIOS) {
  190. expect(1).toBe(1)
  191. return
  192. }
  193. // TODO: harmony 页面隐藏时需要隐藏键盘
  194. if (isHarmony) {
  195. await program.tap({ x: 100, y: 200 })
  196. await page.waitFor(1000);
  197. }
  198. await program.navigateTo("/pages/API/navigator/new-page/new-page-3")
  199. await page.waitFor(2000);
  200. await program.navigateBack()
  201. await page.waitFor(1000);
  202. await page.setData({
  203. focusedForKeyboardHeightChangeTest: true
  204. })
  205. await page.waitFor(2000);
  206. const keyboardHeight = await page.data('keyboardHeight');
  207. console.log("keyboardHeight :", keyboardHeight);
  208. expect(keyboardHeight).toBeGreaterThan(25)
  209. //reset
  210. await page.setData({
  211. focusedForKeyboardHeightChangeTest: false,
  212. keyboardHeight: 0
  213. })
  214. })
  215. it("afterAllTestScreenshot", async () => {
  216. const image = await program.screenshot({
  217. fullPage: true
  218. })
  219. expect(image).toSaveImageSnapshot()
  220. })
  221. it('both set modelValue and value', async () => {
  222. const input2 = await page.$('#both-model-value');
  223. expect(await input2.value()).toEqual("123")
  224. })
  225. });