utils-specs.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { getXctestrunFilePath, getAdditionalRunContent, getXctestrunFileName } from '../../lib/utils';
  2. import { PLATFORM_NAME_IOS, PLATFORM_NAME_TVOS } from '../../lib/constants';
  3. import { withMocks } from '@appium/test-support';
  4. import { fs } from '@appium/support';
  5. import path from 'path';
  6. import { fail } from 'assert';
  7. import { arch } from 'os';
  8. function get_arch() {
  9. return arch() === 'arm64' ? 'arm64' : 'x86_64';
  10. }
  11. describe('utils', function () {
  12. let chai;
  13. before(async function() {
  14. chai = await import('chai');
  15. const chaiAsPromised = await import('chai-as-promised');
  16. chai.should();
  17. chai.use(chaiAsPromised.default);
  18. });
  19. describe('#getXctestrunFilePath', withMocks({fs}, function (mocks) {
  20. const platformVersion = '12.0';
  21. const sdkVersion = '12.2';
  22. const udid = 'xxxxxyyyyyyzzzzzz';
  23. const bootstrapPath = 'path/to/data';
  24. const platformName = PLATFORM_NAME_IOS;
  25. afterEach(function () {
  26. mocks.verify();
  27. });
  28. it('should return sdk based path with udid', async function () {
  29. mocks.fs.expects('exists')
  30. .withExactArgs(path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`))
  31. .returns(true);
  32. mocks.fs.expects('copyFile')
  33. .never();
  34. const deviceInfo = {isRealDevice: true, udid, platformVersion, platformName};
  35. await getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath)
  36. .should.eventually.equal(path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`));
  37. });
  38. it('should return sdk based path without udid, copy them', async function () {
  39. mocks.fs.expects('exists')
  40. .withExactArgs(path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`))
  41. .returns(false);
  42. mocks.fs.expects('exists')
  43. .withExactArgs(path.resolve(`${bootstrapPath}/WebDriverAgentRunner_iphoneos${sdkVersion}-arm64.xctestrun`))
  44. .returns(true);
  45. mocks.fs.expects('copyFile')
  46. .withExactArgs(
  47. path.resolve(`${bootstrapPath}/WebDriverAgentRunner_iphoneos${sdkVersion}-arm64.xctestrun`),
  48. path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`)
  49. )
  50. .returns(true);
  51. const deviceInfo = {isRealDevice: true, udid, platformVersion};
  52. await getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath)
  53. .should.eventually.equal(path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`));
  54. });
  55. it('should return platform based path', async function () {
  56. mocks.fs.expects('exists')
  57. .withExactArgs(path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`))
  58. .returns(false);
  59. mocks.fs.expects('exists')
  60. .withExactArgs(path.resolve(`${bootstrapPath}/WebDriverAgentRunner_iphonesimulator${sdkVersion}-${get_arch()}.xctestrun`))
  61. .returns(false);
  62. mocks.fs.expects('exists')
  63. .withExactArgs(path.resolve(`${bootstrapPath}/${udid}_${platformVersion}.xctestrun`))
  64. .returns(true);
  65. mocks.fs.expects('copyFile')
  66. .never();
  67. const deviceInfo = {isRealDevice: false, udid, platformVersion};
  68. await getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath)
  69. .should.eventually.equal(path.resolve(`${bootstrapPath}/${udid}_${platformVersion}.xctestrun`));
  70. });
  71. it('should return platform based path without udid, copy them', async function () {
  72. mocks.fs.expects('exists')
  73. .withExactArgs(path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`))
  74. .returns(false);
  75. mocks.fs.expects('exists')
  76. .withExactArgs(path.resolve(`${bootstrapPath}/WebDriverAgentRunner_iphonesimulator${sdkVersion}-${get_arch()}.xctestrun`))
  77. .returns(false);
  78. mocks.fs.expects('exists')
  79. .withExactArgs(path.resolve(`${bootstrapPath}/${udid}_${platformVersion}.xctestrun`))
  80. .returns(false);
  81. mocks.fs.expects('exists')
  82. .withExactArgs(path.resolve(`${bootstrapPath}/WebDriverAgentRunner_iphonesimulator${platformVersion}-${get_arch()}.xctestrun`))
  83. .returns(true);
  84. mocks.fs.expects('copyFile')
  85. .withExactArgs(
  86. path.resolve(`${bootstrapPath}/WebDriverAgentRunner_iphonesimulator${platformVersion}-${get_arch()}.xctestrun`),
  87. path.resolve(`${bootstrapPath}/${udid}_${platformVersion}.xctestrun`)
  88. )
  89. .returns(true);
  90. const deviceInfo = {isRealDevice: false, udid, platformVersion};
  91. await getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath)
  92. .should.eventually.equal(path.resolve(`${bootstrapPath}/${udid}_${platformVersion}.xctestrun`));
  93. });
  94. it('should raise an exception because of no files', async function () {
  95. const expected = path.resolve(`${bootstrapPath}/WebDriverAgentRunner_iphonesimulator${sdkVersion}-${get_arch()}.xctestrun`);
  96. mocks.fs.expects('exists').exactly(4).returns(false);
  97. const deviceInfo = {isRealDevice: false, udid, platformVersion};
  98. try {
  99. await getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath);
  100. fail();
  101. } catch (err) {
  102. err.message.should.equal(`If you are using 'useXctestrunFile' capability then you need to have a xctestrun file (expected: '${expected}')`);
  103. }
  104. });
  105. }));
  106. describe('#getAdditionalRunContent', function () {
  107. it('should return ios format', function () {
  108. const wdaPort = getAdditionalRunContent(PLATFORM_NAME_IOS, 8000);
  109. wdaPort.WebDriverAgentRunner
  110. .EnvironmentVariables.USE_PORT
  111. .should.equal('8000');
  112. });
  113. it('should return tvos format', function () {
  114. const wdaPort = getAdditionalRunContent(PLATFORM_NAME_TVOS, '9000');
  115. wdaPort.WebDriverAgentRunner_tvOS
  116. .EnvironmentVariables.USE_PORT
  117. .should.equal('9000');
  118. });
  119. });
  120. describe('#getXctestrunFileName', function () {
  121. const platformVersion = '12.0';
  122. const udid = 'xxxxxyyyyyyzzzzzz';
  123. it('should return ios format, real device', function () {
  124. const platformName = 'iOs';
  125. const deviceInfo = {isRealDevice: true, udid, platformVersion, platformName};
  126. getXctestrunFileName(deviceInfo, '10.2.0').should.equal(
  127. 'WebDriverAgentRunner_iphoneos10.2.0-arm64.xctestrun');
  128. });
  129. it('should return ios format, simulator', function () {
  130. const platformName = 'ios';
  131. const deviceInfo = {isRealDevice: false, udid, platformVersion, platformName};
  132. getXctestrunFileName(deviceInfo, '10.2.0').should.equal(
  133. `WebDriverAgentRunner_iphonesimulator10.2.0-${get_arch()}.xctestrun`);
  134. });
  135. it('should return tvos format, real device', function () {
  136. const platformName = 'tVos';
  137. const deviceInfo = {isRealDevice: true, udid, platformVersion, platformName};
  138. getXctestrunFileName(deviceInfo, '10.2.0').should.equal(
  139. 'WebDriverAgentRunner_tvOS_appletvos10.2.0-arm64.xctestrun');
  140. });
  141. it('should return tvos format, simulator', function () {
  142. const platformName = 'tvOS';
  143. const deviceInfo = {isRealDevice: false, udid, platformVersion, platformName};
  144. getXctestrunFileName(deviceInfo, '10.2.0').should.equal(
  145. `WebDriverAgentRunner_tvOS_appletvsimulator10.2.0-${get_arch()}.xctestrun`);
  146. });
  147. });
  148. });